The documentation for useMemo
says:
You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance
React.memo
and shouldComponentUpdate
have similar warnings.
I have a case, however, where I want a functional component not to update under certain conditions. I am looking for memo-like functionality not for performance, but for the semantic guarantee of there being no update given some conditions. How do I address this issue?
Now you have a good understanding of both of them. Use React. memo to memoize an entire component. Use useMemo to memoize a value within a functional component.
The React useMemo Hook returns a memoized value. Think of memoization as caching a value so that it does not need to be recalculated. The useMemo Hook only runs when one of its dependencies update. This can improve performance.
Store the values you need to memoize in a useRef()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With