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 should support callbacks, same as the typical react setState #544

Open steventnorris-40AU opened 6 years ago

steventnorris-40AU commented 6 years ago

Currently, reflux setState does not support a callback. I believe expected functionality here would be that it worked like or better than the typical React setState.

Suggestion: Set expected completions based on number of subscribed components. While looping setState for all subscribed components, fire off a callback on that component's setState indicating it is complete. When all completions are reached, fire the callback for the reflux store's setState.

I believe this could also be wrapped as a promise instead of a callback. Honestly, I'm not sure why the normal React setState is a callback and not a promise.

BryanGrezeszak commented 6 years ago

Reflux's this.setState does not have a callback because it changes its store state immediately. React only has callbacks as a workaround for the fact that it doesn't. It would be very awkward (and even confusing) to implement a workaround that "works around" a problem that doesn't actually exist in Reflux.

As for your suggestion that it instead call back when all subscribing components have updated their state: that is a major violation of the single most fundamental principle of flux itself (one-way data flow).

If your store logic depends upon knowing the state of subscribed components then you're doing something very different from flux!

However, I definitely see that this should be documented so that people know they don't need callbacks, and will get that in the docs soon.