enpit / fleux

Towards Sane State Management in React

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hooks

objectliteral opened this issue · comments

Currently, unless users want to bring the store into scope, they need to use the withState HOC to connect their components to a store. Potentially, some users might dislike HOCs and would prefer an API that uses Hooks instead.

Suggestion: Design and implement a Hooks API (useStore).

Details about how this API should look like and how it can be implemented are still to be decided.

The Hooks API for fleux includes four functions:

  • useStore exposes the context-dependent store object. It is proxied to only trigger updates on key-changes of keys that are actually read by this component. useStore also exposes dispatch.
  • useSelector acts similar to useStore but takes a function argument that is used as a selectStateProps function.
  • useDispatch just returns the dispatch method of the context-dependent store.
  • useActions exposes named actions.

As a side-comment, I want to point out how surprisingly easy it was to implement this. I am not sure as to why this is: I did not have to worry about parseProps-stuff and the nesting of components like in the withState implementation. Nevertheless, it feels suspicious that this was so easy.