React useMemo Hook Description
The React useMemo
Hook returns a memoized value.
Think of memoization as caching a value so that it does not need to be recalculated.
The useMemo
Hook only runs when one of its dependencies update.
This can improve performance.
The useMemo
and useCallback
Hooks are similar. The main difference is that useMemo
returns a memoized value and useCallback
returns a memoized function.
Performance
The useMemo
Hook can be used to keep expensive, resource intensive functions from needlessly running.
In this example, we have an expensive function that runs on every render.
When changing the count or adding a todo, you will notice a delay in execution.
Please find the Example:
A poor performing function. The expensiveCalculation
function runs on every render:
import { useState } from "react";const useMemoDemo= () => {const [count, setCount] = useState(0);const [todos, setTodos] = useState([]);const calculation = expensiveCalculation(count);const increment = () => {setCount((c) => c + 1);};const addTodo = () => {setTodos((t) => [...t, "New Todo"]);};return (<div><div><h2>My Task</h2>{todos.map((todo, index) => {return <p key={index}>{todo}</p>;})}<button onClick={addTodo}>Add Todo</button></div><hr /><div>Count: {count}<button onClick={increment}>+</button><h2>Expensive Calculation</h2>{calculation}</div></div>);};const expensiveCalculation = (num) => {console.log("Calculating");for (let i = 0; i < 34000000; i++) {num += 1;}return num;};const root = ReactDOM.createRoot(document.getElementById('root'));root.render(<App />);