Advanced Techniques to Optimize React Performance
Tweak Your Apps for Speed
This article will teach you how to optimize your React apps for speed! Many React coders don’t realize that React constantly re-renders components. If you understand what’s going on under the hood, you can adjust your React application to be extremely fast! So get comfy and make some coffee. I’m going to teach you some advanced techniques for optimizing React performance.
Use React.Memo():
React.memo is a higher-order component. It optimizes the performance of functional components in React. It stores the result of a component’s rendering and only re-renders the component if its props change!
Here’s an example of how to use React. memo:
export function MyComponent({ prop1, prop2 }) { return ( <div> <div>{prop1}</div> <div>{prop2}</div> </div> )
}
export const MemoizedComponent = React.memo(MyComponent)
In this example, MyComponent is a functional component. It accepts two props: prop1 and prop2. By wrapping it with the higher-order component React.memo, React only re-renders the component if either of its props changes.
React.memo is useful for:
- …functional components that render often
- …components that take a lot of resources to render
- …components that receive props that won’t change frequently
Keep in mind that React.memo only performs a shallow comparison of its props. If your component receives complex objects or arrays as props, you may need to create a custom comparison function.
Lazy Loading:
React’s Lazy loading feature lets you load components on demand. This is more efficient than loading everything at once when your application starts. Use the React.lazy() function to create a new component that can be lazily loaded. The function takes a dynamic import, which is a JavaScript feature that enables loading modules on demand.
Here’s an example of how to use lazy loading:
const MyLazyComponent = React.lazy(() => import('./MyComponent'))
To use MyLazyComponent, nest it in a loaded component:
function MyApp() { return ( <div> <h1>Welcome to my app!</h1> <React.Suspense fallback={<h1>Loading...</h1>}> <MyLazyComponent/> </React.Suspense> </h1> );
}
In this example, MyApp is a loaded component. When MyApp is rendered, React.Suspense is used to display a fallback UI, such as a loading indicator. Once MyLazyComponent is loaded, it will be substituted for the fallback UI.
Lazy loading is useful for large applications with many components. It can help reduce the initial load time and improve overall performance.
useCallback:
React’s useCallback hook memoizes (stores) functions to optimize the performance of functional components. When a function is wrapped in useCallback, it will only be re-created when its dependencies change.
Here’s an example of how to use useCallback:
function MyComponent() { const [count, setCount] = useState(0); const handleClick = useCallback(() => { setCount(count + 1); }, [count]); return ( <div> <p>Count: {count}</p> <button> onClick={handleClick}>Increment/button> </div> );
}
In this example, the count state variable keeps track of the number of times the button has been clicked. We also have a handleClick function that increments the count state variable when the button is clicked.
We use useCallback to memoize the handleClick function, and specify [count] as its dependency array. This means that the function will be re-created only when count changes, and not on every re-render of the component.
Using useCallback can be particularly useful in scenarios where a function is being passed down to child components as a prop. By memoizing the function with useCallback, we can avoid unnecessary re-renders of the child components that depend on that function.
Note that it’s important to use useCallback only when necessary, as it can add some complexity to your code. Only use it for expensive functions or when passing functions as props to child components.
Optimizing List Performance by Using Keys:
To improve performance in React, developers use the key prop to identify each item in a list of components. When rendering a list of components, using a key can improve performance by helping React identify which items have changed.
Here are three tips for using the key prop effectively:
- Use a unique identifier for the key prop. Instead of using an index as the key, which can cause issues when items are added or removed from the list, a unique identifier should be used for each item in the list.
- Use a stable identifier for the key prop. The same identifier should be used for the same item, even if the order of the list changes. This helps React avoid unnecessary re-rendering.
- Apply the key prop to the outermost element in a component’s return statement. Applying the key prop to an inner element may cause unexpected behavior because React uses the key prop to identify each component.
Here’s an example of how to use key when rendering a list of components:
function MyList(props) { const items = props.items.map(item => ( <MyListItem key={item.id} item={item} /> )); return <ul>{items}</ul>;
}
In this example, MyList is a component that renders a list of MyListItem components. The key prop is set to item.id, which is a unique identifier for each item in the list.
Using key can help improve React performance by minimizing unnecessary re-renders of components in a list. By using a unique and stable key for each item, React can more easily identify which items have changed and need to be updated.
Conclusion
There are more techniques than we have time to talk about in this article, this should get you started. The ones we talked about in this article are:
- React.Memo()
- Lazy Loading
- React’s useCallback hook
- Optimizing List Performance by Using Keys

Tags: javascript, optimization, react