-
-
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
throw if getState, subscribe, or unsubscribe called while dispatching #1569
Conversation
Hey @gaearon, I added checks and updated tests. Let me know your thoughts, thanks! |
var isSubscribed = true | ||
|
||
ensureCanMutateNextListeners() | ||
nextListeners.push(listener) | ||
|
||
return function unsubscribe() { | ||
return function unsubscribe() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: empty spaces
@@ -71,6 +71,14 @@ export default function createStore(reducer, initialState, enhancer) { | |||
* @returns {any} The current state tree of your application. | |||
*/ | |||
function getState() { | |||
if (isDispatching) { | |||
throw new Error( | |||
'You may not call store.getState() while the reducer is executing.' + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should have spaces right after the period, or you’ll end up with executing.The reducer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed with efd0d2e
The I looked around, but didn't see anything specifically mentioning these scenarios in the documentation. Do you think this should be documented too? Here is what I have for // subscribe
'You may not call store.subscribe() while the reducer is executing. ' +
'If you would like to be notified after the store has been updated, subscribe from a ' +
'component and invoke store.getState() in the callback to access the latest state. ' +
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
// unsubscribe
'You may not unsubscribe from a store listener while the reducer is executing. ' +
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.' |
These sound good. |
Note: it’s a breaking change. Since we merged this to master, we can’t release patches now. |
I’m going to revert because we might need to release more patches. I don’t currently have time to work on 4.x but if somebody wants to lead the effort I’m happy to discuss what should go there. |
I can extract the commit back out to |
👍 |
…#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
Done. bb9b86c |
…#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…reduxjs#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…reduxjs#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
…reduxjs#1569) * throw error if getState, subscribe, or unsubscribe called while dispatching * prevent throwing if not subscribed * update getState error message * fix space after period * update subscribe/unsubscribe error messages
I came across this PR while researching the exception
|
@phips28 it's hard to say without seeing the app's component code, but I think you might be doing some async action and then immediately unmounting a |
Looking at the call stack, the unsubscribe is clearly coming from React-Redux's |
I updated some libraries, maybe there was a breaking change somewhere. I added some more logging to locate the problem, but its difficult because I cannot reproduce it. |
This is a un-debug-able bug crash 🙈
But the stacktrace doesnt help me lot.
|
I now disabled minification of the JS bundle and it seems to fix the problem. PROJECT/node_modules/react-native/react.gradle
|
This fixes #1568.