Adding an optional callback argument to the dispatch method provided by withReducer#595
Conversation
Codecov Report
@@ Coverage Diff @@
## master #595 +/- ##
==========================================
+ Coverage 88.18% 88.28% +0.09%
==========================================
Files 49 49
Lines 364 367 +3
==========================================
+ Hits 321 324 +3
Misses 43 43
Continue to review full report at Codecov.
|
…d by `withReducer`
b4e5860 to
6e92f41
Compare
|
Hi, Thank you. |
|
Thank you for the feedback! While I definitely see that
|
|
I havent used withReducer ;-) @wuct what are you think about, may be in absence of middlewares this is not so bad as I wrote? |
|
It'd be great to have middlewares like in There are already some differences in the So I ask - in lieu of middleware support and given that the |
|
My suggestion would be to pass in a callback to the payload, e.g. https://github.com/azz0r/fedsimulator/blob/ppvs/src/actions/data.js#L9 But I believe this is considered bad practise, you'd be expected to use componentWillReceiveProps to check the nextProps change then trigger your "callback". |
|
@therealparmesh @istarkov I don't like the idea to extend API surface for side effects, too. However, as @therealparmesh said, we've already provided this kind of surface in @azz0r IMHO it's not a good idea because impure reducers are harder to test and trace code. |
|
Looks like it is good to be shipped. @istarkov any thought? |
|
Any update on if/when this will be released? It would be helpful in a project where I'm currently using recompose. There are workarounds for sure, but I'm not a fan of having an impure reducer either and |
|
Oops sorry, |
|
Thank you @therealparmesh |
I thought of this after looking at
withState, which providesstateUpdatermethods that can take in callbacks. My rationale for allowing for an optional callback that receives the new state for thedispatchmethod thatwithReducerprovides is that after calculating the new state, you might want to propagate state changes elsewhere. Maybe you need to call a givenonChangeprop or handle some other side effect like that.We could just execute the reducer with the old state and the action to generate the new state outside of the
dispatchcall, but I find it really clunky to calculate the new state twice - once implicitly as a result of thedispatchcall and once explicitly by executing the reducer - in order to address these types of scenarios. I am also not a fan of programmatically detecting state changes and handling the associated side effects vialifecyclemethods.I chose to use a plain callback over a promise solely because I did not see
recomposemaking any real use of promises and I did not want to introduce a new paradigm. I am not attached to this decision.Thank you for this amazing library! Please let me know what you think of this idea.