enpit / fleux

Towards Sane State Management in React

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implicit store bindings

objectliteral opened this issue · comments

Currently when creating a stateful component, users have to explicitly specify which keys should be connected to the state and injected into the component.

Suggestion: It should be possible, to implicitly use keys from the store without providing them to withState. fleux should figure out which keys to bind and which components to update without being explicitly told.

I roughly know how to do this, but I am not entirely sure about how to expose this to users. It would probably require them to not access the keys as props, but as object properties on a store object (because at some point, we need to intercept these accesses). A reference to that store thus has to be present. This could be injected as a prop or it has to be brought into scope by the user himself.

Merged into develop as of e7d03ec.

To summarize what happened here:

withState now takes a component as an argument (or no argument and returns a factory as before) and returns a HOC that injects a store prop. The store is proxied twice: Each HOC has its own get trap that curries calls with a reference to the component, which is used to track subscriptions. The second trap is inside the store definition and calls setState on all subscribed components.

The legacy code path (readWriteHOC) was removed to unify the paths for implicitly and explicitly bound props. withState now still calls parseProps and takes care of injecting props but they are not placed into a wrapper state but resolved via the store. The store retrieval is done by the same function that does it for components that use implicitly bound props.

Surely, there is room for improvement with respect to some of these functionality, but that will be discussed in new issues.