useCallback

React’s useCallback hook is another tool for performance optimization. Unlike useMemo which caches the result of a function, useCallback focuses on memorizing the function itself. UseCallback allows you to memorize a function, meaning it returns the same function reference as long as its dependencies haven’t changed. Here’s how it helps.

Syntax:

const memoizedFunction = useCallback(() => {
// Function body
}, [dependencies]);

Example: Below is the code example:

JavaScript
// index.js 

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

function ParentComponent(props) {
    const [data, setData] = useState([]);

    const handleClick = useCallback(() => {
        // Perform action using data
    }, [data]);

    return (
        <div>
            <ChildComponent handleClick={handleClick} />
            <button onClick={() => setData([...data, Math.random()])}>
                Add Item
            </button>
            {
                data.map((val) => (
                    <li key={val}>{val}</li>
                ))
            }
        </div>
    );
}

function ChildComponent(props) {
    return <button onClick={props.handleClick}>Click me</button>;
}

const root = ReactDOM.createRoot(
    document.getElementById('root')
);
root.render(
    <div id='main_container'>
        <div>
            <img src=
'https://media.w3wiki.org/gfg-gg-logo.svg' alt='gfg_logo' />
        </div>
        <ParentComponent />
    </div>
);

Output:

useCallback Project Output

Mastering Performance Optimization Techniques with React Hooks

In this article, we will explore advanced performance optimization techniques using React Hooks. We’ll delve into memoization, callback optimization, preventing unnecessary renders, state updates, and more. By mastering these techniques, developers can significantly enhance the performance of their React applications.

Similar Reads

1. UseMemo:

The useMemo hook in React.js allows you to memorize the result of a function, meaning it caches the output and only re-evaluates the function when its dependencies change. This can improve performance by avoiding unnecessary computations on every render....

2. useCallback

React’s useCallback hook is another tool for performance optimization. Unlike useMemo which caches the result of a function, useCallback focuses on memorizing the function itself. UseCallback allows you to memorize a function, meaning it returns the same function reference as long as its dependencies haven’t changed. Here’s how it helps....

3. React.memo

In React, React.memo is a Higher-Order Component (HOC) that helps improve performance by preventing unnecessary re-renders of functional components. It use Memoization technique to optimize the performance of React components by caching the result of the component’s rendering and reusing it if the component’s props remain unchanged. It leads to lower memory usage due to less re-render. Here’s a breakdown of how it works:...

4. UseEffect:

UseEffect is a powerful hook in React for performing side effects like data fetching, subscriptions, or setting up timers. By specifying dependencies, you can control when the effect runs. If any of the dependencies change between renders, the effect function will be called again and if dependencies array is empty it runs only once. However, if used incorrectly, it can lead to unnecessary re-renders, impacting your application’s performance...

5. React Virtualization

Rendering large lists in React can lead to performance issues due to large number of DOM elements being created and managed. React Virtualization is a technique used to optimize the rendering of large lists by only rendering the items that are currently visible on the screen. This significantly reduces the memory footprint and improves the performance of your application....

6. useRef:

UseRef() is a hook provided by React that creates a mutable reference object which persists across renders of a functional component. It returns a single mutable value object ({current}) that can be updated without causing re-renders. useRef() is commonly used to access or store values that persist between renders without triggering component re-renders....