reflux / refluxjs

A simple library for uni-directional dataflow application architecture with React extensions inspired by Flux
BSD 3-Clause "New" or "Revised" License
5.36k stars 330 forks source link

Reflux.Store setState issue #520

Closed LiangZugeng closed 4 years ago

LiangZugeng commented 7 years ago

I'm using Reflux in ES6 style, that's Reflux.Store + Reflux.Component, everything works fine until I today changed the setState({ someState: someValue }) call in Reflux store to setState(previousState => { return { someState: someValue }; }) call.

The problem was that in the 2nd form of setState call, the value of someState got updated in Reflux.Component instead of Reflux.Store despite that I was calling it in the store.

My question is: if Reflux only supports setState calls with a object but not a function in the store.

BTW, I really need to call setState in the store to update some states in the store in order to persist them during page navigation. Please advise.

BryanGrezeszak commented 7 years ago

Currently Reflux stores do not have any support for the state => newState function type of state setting.

I supposed the result of that would have been it sending the actual function to the component rather than calling it for state of the store first, then sending the result to the component. Which would probably lead to the effect you're describing.

For the sake of idiomatic API, it definitely would be a good addition. So I'll add it as a planned enhancement.

However, in the mean time, as an FYI: there's no actual technical need for it. So you can move on with your project without it.

The stale-state issue that it solves in React doesn't exist in Reflux stores. React can't guarantee that state updates in a component will be synchronous, but state updates within a Reflux store are always synchronous (within the store anyway, the pushing of that state to the components still is handled by React, of course, but that's not what's relevant at the moment).

They don't have the overhead that component updates have, so there's no need to batch them or anything like React does to perform better. Therefore there's is no need to worry about stale state in a Reflux store. So you can just set the state and not worry about state being stale even if you're using current state as part of the update to the new state.

That said, I'm definitely gonna update to support the syntax...it's just plain the good idiomatic way to do it to keep it similar to React. Just saying...don't let it hold you back in your project :)