-
-
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 new value on each call even equalityFn returns true #1654
Comments
This is specifically because you're passing in a new selector function reference on each render pass. We always re-run the selector if the selector is a new reference, because your code could have changed what it's selecting entirely (ie, going from If you were to extract the selector function definition outside the component, or memoize creating it, you'll see the same array result being returned. |
Sorry but I still think In many cases it's hard to keep selector reference equal, mostly when we select state from props: const post = useSelector(s => {
const post = s.posts[props.id];
const author = s.users[post.author];
return {...post, author};
}); I believe when user passes a |
Mmm... I can see this as a potential point, yeah. Not something I have time to change myself right now, but if you or someone else wants to put up a PR, we can look at it. |
Hey @otakustay! Thank you for opening this issue—I just had the same confusing problem, where I assumed that the equalityFn would be applied in both cases! This was what my code looked like: const items = useSelector(state => todoIds.map(id => state.entities.todos[id]), shallowEquals);
useMemo(() => doSomethingExpensiveWith(items), [items]); and I found to my surprise that the @otakustay Let me know if you need any help with that patch—would love to get this over the finish line! |
@nightpool I have created #1660 days ago, waiting for a review or merge |
I ran into this as well where we are using a selector for constructing translation string keys, but due to some race conditions they can get malformed during transitions. We still want to reuse the selector logic so I was hoping to just override the equalityFn with It seems like @otakustay's PR should fix that since that's exactly what they've done in tests. |
Co-authored-by: nightpool <[email protected]> Co-authored-by: Tim Dorr <[email protected]>
What is the current behavior?
We have some confusing logs in console like:
This indicates the return value of
useSelector
changes even itsequalityFn
returnstrue
.What is the expected behavior?
In
DisplayValueList
component, the valuemustChange
can change on every press, but the nextuseSelector
should return a constant value because its selector function returns a static array and itsequalityFn
is set to an array equal function, the console log should be clean.When the first
useSelector
is removed, the console output is clean as expected, this is even more confusing to me.The text was updated successfully, but these errors were encountered: