Dmitry Minkovsky
Dmitry Minkovsky

Reputation: 38143

How do I use React.memo or useMemo with a semantic guarantee?

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?

Upvotes: 10

Views: 1374

Answers (1)

Will Jenkins
Will Jenkins

Reputation: 9787

Store the values you need to memoize in a useRef()

Upvotes: 5

Related Questions