-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
useSelector returns stale value in parent component if child component dispatches action updating that value during render #1599
Comments
This is expected behavior because you are not wrapping a side effect during render in useEffect. This is just how React works and would happen the same if you used a global variable. |
I've updated the example to replace the child comment's side effects on render with a class component with side effects on componentDidMount and componentDidUpdate, and it yields similar issues. https://codesandbox.io/s/race-condition-x1h1d
|
The last one should be fixed by #1536. (I didn't notice that it's not only with You could also use |
What is the current behavior?
Demo: https://codesandbox.io/s/race-condition-x1h1d
In this example, our parent component consumes a value from the store, and a child component on render calls an action to update that value multiple times. We see the child component receives the updated value, while the parent component does not after the first update. The parent component's useSelector instance doesn't invoke the selector function and just returns the cached value.
Wrapping the child component's action dispatch in a useEffect solves the issue.
Defining the selector in-line so that each render passes a different function reference, forcing useSelector to call the selector again also works around the issue.
The real-life use case for this pattern is that we have a redirect caused by rendering a react-router redirect component, and this causes an update to the redux store which is storing the current location thru connected-react-router, and the parent component to this consumes location from the store.
What is the expected behavior?
We would expect the parent component's useSelector instance to return the same value from the store as the child component's
Which versions of React, ReactDOM/React Native, Redux, and React Redux are you using? Which browser and OS are affected by this issue? Did this work in previous versions of React Redux?
The versions used in the code sandbox are:
react: 16.13.1
react-dom: 16.8.3
react-redux: 7.1.3
redux: 4.0.5
The text was updated successfully, but these errors were encountered: