useMemo is a React hook used to remember the result of a calculation between renders.
Normally, whenever a component renders again, calculations inside the component are performed again.
If a calculation is expensive, repeating it unnecessarily can affect performance.
useMemo helps avoid repeating that calculation when its dependencies have not changed.
It is useful for:
- Expensive calculations.
- Filtering or sorting large arrays.
- Calculating derived values from state or props.
- Avoiding unnecessary recalculations.
Syntax:
import { useMemo } from "react"; const result = useMemo(() => { return calculation; }, [dependencies]); Syntax explanation:
Part Meaning
useMemo React hook that remembers a calculation result.
() => {} The function that performs the calculation.
return Returns the calculated value.
[dependencies] Values that determine when the calculation runs again.