Back to Snippets

Optimizing React Component Performance with the Use Memo Hook

JavaScript JavaScript / jQuery May 20, 2026

This code snippet demonstrates how to use the React use Memo hook to optimize component performance by memoizing expensive function calls and preventing unnecessary re-renders. It is particularly useful when dealing with complex computations or API calls that can slow down your application. The use Memo hook is used to memoize the result of an expensive function call, and the dependency array is updated to prevent stale values from being returned. By utilizing this hook, developers can significantly improve the performance of their React applications.

Snippet Stats

Lines 24
Characters 636
Read 1 min
javascript • 24 lines
import { useMemo, useState } from 'react';

const ExampleComponent = () => {
    const [count, setCount] = useState(0);
    const expensiveCalculation = (count) => {
        // Simulate an expensive calculation
        for (let i = 0; i < 10000000; i++) {
            // Do something expensive
        }
        return count * 2;
    };

    const memoizedCalculation = useMemo(() => expensiveCalculation(count), [count]);
    
    return (
        <div>
            <p>Count: {count}</p>
            <p>Expensive Calculation: {memoizedCalculation}</p>
            <button onClick={() => setCount(count + 1)}>Increment Count</button>
        </div>
    );
};

export default ExampleComponent;

Found an issue with this snippet? Help us improve by reporting it. Report it →

Related Snippets

View all
JavaScript
This React hook handles asynchronous data fetching from APIs, managing loading, error, and success states. It utilizes the useEffect and useCallback h...

React Hook For Asynchronous Data Fetching With Error Handling

JavaScript
This React code snippet demonstrates an optimization technique using memoization and callbacks to prevent unnecessary re-renders. It is particularly u...

React Optimization Technique Using Memoization And Callbacks

JavaScript
This snippet provides a reusable React hook for managing asynchronous operations, including error handling and loading states. Use this hook when you ...

Custom React Hook For Handling Async Operations