Web Development React.js Subjective
Sep 28, 2025

What is the useCallback hook and when to use it?

Detailed Explanation

useCallback memoizes functions to prevent unnecessary re-renders. Use it when passing callbacks to child components that depend on specific values.

function Parent({ items }) {
  const [count, setCount] = useState(0);
  
  const handleClick = useCallback((id) => {
    // Function only recreated if count changes
    console.log(`Clicked ${id}, count: ${count}`);
  }, [count]);
  
  return (
    <div>
      {items.map(item => (
        <Child key={item.id} onClick={handleClick} />
      ))}
    </div>
  );
}
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback