jumpsuit / jumpstate

Jumpstate is a simple and powerful state management utility for Redux.
MIT License
425 stars 22 forks source link

Fix State reducer to use state hydration from redux #10

Closed e-r-w closed 7 years ago

e-r-w commented 7 years ago

Currently, when you call createStore() with an initialState argument, the initial object of each State overides the initialState passed down from redux. Not sure if this is intentional, but this means that you cannot hydrate the state tree at boot (i.e from localstorage, isomorphic rendering etc).

Loving your work though guys! This stuff rocks.

tannerlinsley commented 7 years ago

Also, thanks! We can only hope its making life easier :)

e-r-w commented 7 years ago

Yeah so the reducer would be a pure function then. In the event that the action matches the reducer, it will behave the same as before. However if the action is not matched it will return the state passed in or the initial state the user has specified in the event that the state passed in is undefined

tannerlinsley commented 7 years ago

We would need to formulate a way to get the last known state from the reducer then, else we deprecate that functionality for many users.

e-r-w commented 7 years ago

The reducer's job is to determine the next state given a previous state and an action. You can get the current state with getState() but you shouldn't be relying on the reducer to store state, that's the store's responsibility. You could maybe add a flag that preserves the current functionality, and default it to off in a future release but I would certainly deprecate that functionality as it will cause side effects (such as the issue I've ran into)

tannerlinsley commented 7 years ago

How about a reserved hanging method on the reducer:

state() // pure
state.getState() // will return the last-known state to pass through
e-r-w commented 7 years ago

sounds good, bumped PR with changes

e-r-w commented 7 years ago

thats correct, it will emit an event labelled "@@REDUX/init" or something similar

tannerlinsley commented 7 years ago

Thanks @e-r-w! You rock! I'll get a release out asap.