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

setState callback not executed in store #542

Closed xelmido closed 6 years ago

xelmido commented 6 years ago

In some scenarios i need to use a callback of the setState. But, it doesn't trigger the callback in a store. Am i do something wrong? Or is it a bug in the Reflux.store. E.g:

onSetSomething(id, resolve) { this.setState({ something: Id } () => { resolve(); }); }

kriscarle commented 6 years ago

It doesn't look like refluxjs supports a callback in setState. It is providing it's own setState method in between React's setState, needed because it has to loop through all of the components listening to the store. Since each component's setState is async, for this to work the way you'd expect it to with regular React it would have to wait for all the component callbacks, one for each component that listens to the store, not ideal if you really only care about a particular component.

I would do this by watching for the change in the component, for example:

componentDidUpdate(prevProps, PrevState){
 if(prevState.something !== this.state.something) {
   //do your resolve action here
  alert('something has changed');
 }
}
xelmido commented 6 years ago

I already used the componentDidUpdate. It did the trick for my case.

Thanks for clearing it up I am closing this for now!