React · Senior · Question 28 of 75

Does useMemo guarantee the computation won't repeat with the same dependencies?

Explanation

The docs explicitly call useMemo a performance optimization: React may throw away the cache (e.g. for hidden components or in future versions). If code breaks without useMemo, look for a logic problem. For a guaranteed constant value, use useState(() => init) or useRef.

Further reading

Problem with a question

React · Senior · question 28

Test yourself

Take the React · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz