React.js useCallback()

useCallback is a React.js Hook. When you have a component with a child repeatedly re-rendering and you pass a callback to it. UseCallback returns a memorized callback. React.useCallback gets a function as the first argument and a dependencies array as a second argument. It will return a new value when the dependencies value changes.

 

import React, { useCallback } from 'react';


function MyComponent() {

  const handleClick = useCallback(() => {

    // handle the click event

  }, []);


  return <MyChild onClick={handleClick} />;

}

We should not optimize unwanted re-renders without seeing the after effect, optimization always comes with a cost. React.useCallback returns a memorized callback.