9951 explained code solutions for 126 technologies


reactjsHow do I use the useCallback hook in React?


The useCallback hook in React is used to create a callback function that is memoized. This means that the callback will only be recreated when one of its dependencies has changed. This is useful for performance optimization, as it prevents unnecessary re-renders when a component is re-rendered.

An example of using useCallback looks like this:

const MyComponent = () => {
  const [count, setCount] = useState(0);

  const increment = useCallback(() => {
    setCount(count => count + 1);
  }, [setCount]);

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={increment}>Increment</button>
    </div>
  );
};

In this example, useCallback is used to create a memoized version of the increment function. This prevents unnecessary re-renders when the component is re-rendered.

The code can be broken down as follows:

  1. const MyComponent = () => { - This is the start of the component.
  2. const [count, setCount] = useState(0); - This uses the useState hook to create a state variable called count with an initial value of 0.
  3. const increment = useCallback(() => { - This creates the increment function using the useCallback hook.
  4. setCount(count => count + 1); - This is the body of the increment function, which updates the count state variable by one.
  5. }, [setCount]); - This is the end of the useCallback hook, and it passes the setCount function as a dependency so that the increment function will be re-created when setCount changes.
  6. <button onClick={increment}>Increment</button> - This is a button which calls the increment function when it is clicked.

For more information about the useCallback hook, see the React documentation.

Edit this code on GitHub