React · Senior · Question 28 of 75
Does useMemo guarantee the computation won't repeat with the same dependencies?
- ✓Correct answer: No: React may discard the cache; code must work without
useMemotoo - 2Yes: it's a semantic guarantee you can rely on for effects
- 3Yes, but only if dependencies are primitives rather than objects
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.