-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forbid getState() and subscribe() while the reducer is being called #1568
Comments
I can work on this. |
👍 Don’t forget the unsubscribe too (the function being returned from |
Thanks! Let’s continue the discussion in #1569. |
@gaearon I realize that this has been resolved, but since this is the clear Google result when people search this issue, I feel like this is an appropriate place to make this comment. First off, thank you for your work on Redux. You mention "They make the reducer impure, and you can always pass the store state down as the third argument if this is something you’re interested in." Could you explain where this third argument might be invoked? Citing the official documentation on using combineReducers, it seems like there's no clear way to pass a third parameter. The next article in the documentation, titled "Beyond combineReducers", does suggest some patterns.
The way I'm guessing this works (but it's not entirely clear) is that: a) Is this understanding correct? In practice, would anything need to be modified in the dispatch call or would the wholeState automatically be inferred? The other way I tried it, passing around store.getState().otherSliceINeeded, was unclean in various ways, not to mention the fact that in some cases, it would require dispatching another action in sequence to update the otherSlice. [1] [1] Although it seems like with the new |
@zackster : bit of a thread necro here :) but yes, the point is that you'd need to break out of the default paradigm used by |
This is an anti-pattern, first found here: reduxjs/redux-devtools#264.
I think we should protect against
store.getState()
calls from inside the reducer. They make the reducer impure, and you can always pass the store state down as the third argument if this is something you’re interested in.I think we should forbid this pattern just like we forbid
dispatch()
while the reducer is running.Same goes for
subscribe()
which should never be called from inside a reducer.A pull request to implement this is welcome.
The text was updated successfully, but these errors were encountered: