FormidableLabs / freactal

Clean and robust state management for React and React-like libs.
MIT License
1.65k stars 46 forks source link

Possible error in the examples in the docs relating to effects #17

Closed richsilv closed 7 years ago

richsilv commented 7 years ago

The README gives the following examples for effects:

addOne: () => state => Object.assign({}, state, { counter: state.counter + 1 })
/* vs */
addOne: () => state => Promise.resolve(Object.assign({}, state, { counter: state.counter + 1 }))
/* vs */
addOne: () => state => new Promise(resolve => resolve(Object.assign({}, state, { counter: state.counter + 1 })))

To put it explicitly, the value you provide for each key in your effects object is:

  1. A function that takes in some arguments (we'll cover those shortly) and returns...
  2. A promise that resolves to...
  3. A function that takes in state and returns...
  4. The updated state.

...

So, you might define the following effect:

updatePosts: () => fetch("/api/posts")
  .then(result => result.json())
  .then(({ posts }) => state => Object.assign({}, state, { posts }))

The fetch example matches the flow described (and makes sense), but the first set of examples above are functions that take some arguments that return functions that take in state and return promises. I.e. they have 2 and 3 the wrong way round. Is this also valid?

Happy to PR if the addOne examples need changing, or to shut up if I've just misunderstood.

divmain commented 7 years ago

Thanks for pointing this out! It was definitely incorrect :) @zebulonj updated the README as part of #21, so I'm closing this one out.