Pie Logo
useCallback
by Tomas Trescak

PresentationInteractive, narrated presentation of the section content.

ContentDetailed description of the section content.

Understanding useCallback in React

The useCallback hook in React is used to memoize functions so that they are not recreated on every render. This helps in optimizing performance, especially in scenarios where the function is passed as a prop to child components or used in expensive computations.


Why useCallback?

Every time a component re-renders, all its functions are recreated, even if their logic hasn’t changed. This can lead to:

  1. Unnecessary re-renders of child components.
  2. Performance issues in complex apps.

By memoizing a function with useCallback, you ensure the function retains its identity unless its dependencies change.


How useCallback Works

const memoizedCallback = useCallback(() => {
  // Your function logic here
}, [dependencies]);
  • useCallback returns the same function instance as long as the dependencies don’t change.
  • When dependencies change, a new function is created.

Key Use Cases

  1. Preventing unnecessary renders of child components.
  2. Optimizing event handlers in large or complex components.

Basic Example

Here’s an example of how useCallback can prevent unnecessary re-renders:

Without <strong>useCallback</strong>:

import React, { useState } from "react";

const Child = React.memo(({ onClick }) => {
  console.log("Child rendered");
  return <button onClick={onClick}>Click Me</button>;
});

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

  const handleClick = () => {
    console.log("Button clicked");
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child onClick={handleClick} />
    </div>
  );
};

export default Parent;

Behavior:

  • Every time the Parent re-renders (when count changes), the handleClick function is recreated.
  • This causes the Child component to re-render unnecessarily, even though its props haven’t changed.

With <strong>useCallback</strong>:

jsx

Copy code

import React, { useState, useCallback } from "react";

const Child = React.memo(({ onClick }) => {
  console.log("Child rendered");
  return <button onClick={onClick}>Click Me</button>;
});

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

  const handleClick = useCallback(() => {
    console.log("Button clicked");
  }, []); // No dependencies, so the function is memoized and won't change

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child onClick={handleClick} />
    </div>
  );
};

export default Parent;

Behavior:

  • The handleClick function is memoized using useCallback.
  • The Child component doesn’t re-render unless handleClick changes, which happens only if its dependencies change (none in this case).

Advanced Example: With Dependencies

When a function depends on state or props, you can include them in the dependency array.

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

  const handleClick = useCallback(() => {
    console.log(`Count: ${count}`);
  }, [count]); // Recreate the function only if `count` changes

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={handleClick}>Log Count</button>
    </div>
  );
};

Behavior:

  • The handleClick function is recreated only when count changes, ensuring it always logs the latest value of count.

When to Use useCallback

  1. Passing Functions as Props:
    • Prevent unnecessary re-renders of memoized child components.
  2. Event Handlers:
    • Optimize frequently used handlers like onClick, onChange, etc.
  3. Expensive Functionality:
    • Memoize expensive functions that are called repeatedly in the render cycle.

When Not to Use useCallback

  • Overuse: Don’t wrap every function in useCallback. Use it only when there’s a clear performance benefit.
  • Simple Apps: In small apps, function recreation is rarely a performance bottleneck.

Summary

  • Purpose: Memoize functions to prevent unnecessary re-creations and improve performance.
  • Syntax: useCallback(() => {...}, [dependencies])
  • Key Use Cases: Prevent child re-renders, optimize event handlers, memoize expensive functions.
  • Caution: Use it judiciously; overuse can complicate code without significant benefits.

Let me know if you’d like a deeper dive or more examples! 🚀

Discuss with OthersAsk questions, share your thoughts, and discuss with other learners.

Loading discussion ...
Full Stack Development
Introduction
Generative AI
🍰 Player
💼 Editor
Setup
React Fundamentals
Next.js
Advanced React
Databases
React Hooks
forwardRef
useImperativeHandle
useCallback
Quiz: useCallback
useId
useReducer
useTransition
useDeferredValue
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    useCallback
    by Tomas Trescak

    PresentationInteractive, narrated presentation of the section content.

    ContentDetailed description of the section content.

    Understanding useCallback in React

    The useCallback hook in React is used to memoize functions so that they are not recreated on every render. This helps in optimizing performance, especially in scenarios where the function is passed as a prop to child components or used in expensive computations.


    Why useCallback?

    Every time a component re-renders, all its functions are recreated, even if their logic hasn’t changed. This can lead to:

    1. Unnecessary re-renders of child components.
    2. Performance issues in complex apps.

    By memoizing a function with useCallback, you ensure the function retains its identity unless its dependencies change.


    How useCallback Works

    const memoizedCallback = useCallback(() => {
      // Your function logic here
    }, [dependencies]);
    
    • useCallback returns the same function instance as long as the dependencies don’t change.
    • When dependencies change, a new function is created.

    Key Use Cases

    1. Preventing unnecessary renders of child components.
    2. Optimizing event handlers in large or complex components.

    Basic Example

    Here’s an example of how useCallback can prevent unnecessary re-renders:

    Without <strong>useCallback</strong>:

    import React, { useState } from "react";
    
    const Child = React.memo(({ onClick }) => {
      console.log("Child rendered");
      return <button onClick={onClick}>Click Me</button>;
    });
    
    const Parent = () => {
      const [count, setCount] = useState(0);
    
      const handleClick = () => {
        console.log("Button clicked");
      };
    
      return (
        <div>
          <p>Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>Increment</button>
          <Child onClick={handleClick} />
        </div>
      );
    };
    
    export default Parent;
    

    Behavior:

    • Every time the Parent re-renders (when count changes), the handleClick function is recreated.
    • This causes the Child component to re-render unnecessarily, even though its props haven’t changed.

    With <strong>useCallback</strong>:

    jsx

    Copy code

    import React, { useState, useCallback } from "react";
    
    const Child = React.memo(({ onClick }) => {
      console.log("Child rendered");
      return <button onClick={onClick}>Click Me</button>;
    });
    
    const Parent = () => {
      const [count, setCount] = useState(0);
    
      const handleClick = useCallback(() => {
        console.log("Button clicked");
      }, []); // No dependencies, so the function is memoized and won't change
    
      return (
        <div>
          <p>Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>Increment</button>
          <Child onClick={handleClick} />
        </div>
      );
    };
    
    export default Parent;
    

    Behavior:

    • The handleClick function is memoized using useCallback.
    • The Child component doesn’t re-render unless handleClick changes, which happens only if its dependencies change (none in this case).

    Advanced Example: With Dependencies

    When a function depends on state or props, you can include them in the dependency array.

    const Parent = () => {
      const [count, setCount] = useState(0);
    
      const handleClick = useCallback(() => {
        console.log(`Count: ${count}`);
      }, [count]); // Recreate the function only if `count` changes
    
      return (
        <div>
          <p>Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>Increment</button>
          <button onClick={handleClick}>Log Count</button>
        </div>
      );
    };
    

    Behavior:

    • The handleClick function is recreated only when count changes, ensuring it always logs the latest value of count.

    When to Use useCallback

    1. Passing Functions as Props:
      • Prevent unnecessary re-renders of memoized child components.
    2. Event Handlers:
      • Optimize frequently used handlers like onClick, onChange, etc.
    3. Expensive Functionality:
      • Memoize expensive functions that are called repeatedly in the render cycle.

    When Not to Use useCallback

    • Overuse: Don’t wrap every function in useCallback. Use it only when there’s a clear performance benefit.
    • Simple Apps: In small apps, function recreation is rarely a performance bottleneck.

    Summary

    • Purpose: Memoize functions to prevent unnecessary re-creations and improve performance.
    • Syntax: useCallback(() => {...}, [dependencies])
    • Key Use Cases: Prevent child re-renders, optimize event handlers, memoize expensive functions.
    • Caution: Use it judiciously; overuse can complicate code without significant benefits.

    Let me know if you’d like a deeper dive or more examples! 🚀

    Discuss with OthersAsk questions, share your thoughts, and discuss with other learners.

    Loading discussion ...
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    forwardRef
    useImperativeHandle
    useCallback
    Quiz: useCallback
    useId
    useReducer
    useTransition
    useDeferredValue
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    No hints available.

    No hints available.

    Pie Logo
    useCallback
    by Tomas Trescak
    Pie Logo
    useCallback
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    forwardRef
    useImperativeHandle
    useCallback
    Quiz: useCallback
    useId
    useReducer
    useTransition
    useDeferredValue
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    forwardRef
    useImperativeHandle
    useCallback
    Quiz: useCallback
    useId
    useReducer
    useTransition
    useDeferredValue
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics