-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
selector in useSelector called one more time after forceUpdate inside hook #1697
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
Comments
Can you put together a CodeSandbox that reproduces the issue? |
Yes, try this https://codesandbox.io/s/react-redux-twice-selector-r43fy I think calling selector twice may prevent stale selectedState if selector use data from other useSelector in the same component |
@markerikson please, check CodeSandbox above You can close this issue if it's correct behavior
It |
@markerikson Do you happen to remember why it's implemented this way? We do two calls because one is run on the subscription callback to check if we need to re-render: react-redux/src/hooks/useSelector.js Lines 65 to 83 in 010c3ee
If that's the case, then we re-render and the selector fires again to provide the value back to the component: react-redux/src/hooks/useSelector.js Line 36 in 010c3ee
We could probably avoid that by also setting |
Yeah, I glanced at the repro briefly, and saw that the reason for the duplicate selector call is that I'm not sure if it's an oversight, or a concern about mutating FWIW, I had a conversation with Dan yesterday where he suggested that "in practice most stuff 'just works' with CM" - ie, yes all the concerns about "tearing" that they've warned about are valid, but in practice it's not actually all that big of an impact. Given that we are trying to track |
I miss |
Fixes #1697 We can avoid running the selector twice by setting the latestStoreState at the same time as setting the latestSelectedState. If the store state hasn't changed, then the selector won't as well (since it's pure).
I can try to put out a 7.2.4 in the next couple days. |
Hey, I was using Suspense and a custom hook to render components asynchronously with async data fetching redux actions and I seem to have run into an issue directly related to this thread. I get the error |
@phantomcosmonaut no, that shouldn't have anything to do with this issue. This is about a selector re-running when the component is already rendering. What you're describing is a store subscription causing a component to render in the first place. If you're seeing problems with React-Redux and Suspense, please file a new issue with a reproduction so we can look into it. |
What is the current behavior?
When state changed, selector recomputes, and value sets into latestSelectedState. Component will re-render, and selector recomputes again, but its value already in latestSelectedState.current
(selector is arrow function -
const data = useSelector((state) => state.data);
)What is the expected behavior?
Selector recomputes once
I can open PR to fix this, but I don't know, may be selector specially calls twice for some reasons
The text was updated successfully, but these errors were encountered: