DEV Community

Discussion on: What is the use of UseMemo

Collapse
 
lexlohr profile image
Alex Lohr

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).

Collapse
 
johnbabu021 profile image
john • Edited

Thats a Gold ✨