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
memo equality check function overrides state #14972
Comments
https://codesandbox.io/s/943916znlp |
Custom equality checks must include all props you care about. Including callbacks. (This is why generally you shouldn't supply your own comparison function — it's easy to make a mistake!) The mistake you're making is that you're ignoring changes to If I fix your equality function, it works: const equalityCheck = (prevProps, nextProps) =>
- prevProps.comparedProp === nextProps.comparedProp;
+ prevProps.comparedProp === nextProps.comparedProp &&
+ prevProps.onSelect === nextProps.onSelect; https://codesandbox.io/s/62961yk72z You can also The most idiomatic fix to this is |
Many thanks for your elaborate explanation! I understand it now, hopefully others who make this same mistake will find their answers here as well and thanks @iskandar17 for chiming in |
Do you want to request a feature or report a bug?
BUG
What is the current behavior?
When a component is wrapped with
React.memo
and is provided 2nd argument - equality check function, it overrides state in the parent componentWhat is the expected behavior?
It should not override the state in parent component
Reproduction and detailed explanation of the bug is within this codesandbox:
https://codesandbox.io/s/4j6ownx8x0
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Codesandbox
Note: I spent few hours verifying that this is a genuine bug and not a due to my misunderstanding, but I apologize if it is the latter.
The text was updated successfully, but these errors were encountered: