The useMemo hook has two functions: keeping object references and processing derived state.
Imagine you have a component that requires an object as argument that is derived from two properties. You want that object only to change if one or both properties changes, so you use useMemo to make sure it doesn't.
Or maybe you have a property that is calculated or otherwise derived from other properties, but computing it is very costly. You can use useMemo to ensure that the computation is only run if a value has changed.
Other frameworks have similar solutions. For example, Solid.js has a createMemo method for derived state (since it evaluates components only once to improve performance and compiles updates into side effects, keeping references is not a use case).
I am a Self Thaught Front End Developer ReactJS contributing to opensource projects make fun in writing blogs and opensourcing building websites and selling
The
useMemohook has two functions: keeping object references and processing derived state.Imagine you have a component that requires an object as argument that is derived from two properties. You want that object only to change if one or both properties changes, so you use
useMemoto make sure it doesn't.Or maybe you have a property that is calculated or otherwise derived from other properties, but computing it is very costly. You can use
useMemoto ensure that the computation is only run if a value has changed.Other frameworks have similar solutions. For example, Solid.js has a
createMemomethod for derived state (since it evaluates components only once to improve performance and compiles updates into side effects, keeping references is not a use case).Thats a Gold ✨