Forem

Cover image for React Performance: Preventing Unnecessary Re-renders
Oğuzhan Ağır
Oğuzhan Ağır

Posted on

React Performance: Preventing Unnecessary Re-renders

The original and complete version of this article is available on the ASP.NET Zero blog:
https://aspnetzero.com/blog/react-performance-and-preventing-unnecessary-renders

React applications can sometimes feel slow even when everything appears to work correctly. The UI renders, no errors occur, yet interactions feel sluggish. Buttons respond with slight delays, scrolling may stutter, and typing can occasionally feel laggy.

In many cases, the underlying reason is unnecessary component re-renders.

React does not rebuild the entire DOM on every update. Instead, when a component renders:

• The component function executes again
• React generates a new virtual tree
• React compares it with the previous render
• Only the necessary DOM updates are applied

This means a re-render does not necessarily mean a DOM update. However, every render still executes the component function again, which may involve expensive calculations, large lists, or complex component trees.

A Common Cause of Performance Issues

One of the most common causes of unnecessary renders is placing state too high in the component tree.

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

  return (
    <>
      <button onClick={() => setCount(count + 1)}>
        {count}
      </button>
      <HeavyComponent />
    </>
  );
};
Enter fullscreen mode Exit fullscreen mode

Even though HeavyComponent does not use the count state, it will still re-render whenever the parent component renders.

A better approach is state colocation, meaning state should live in the lowest possible component that actually needs it.

Memoization and Referential Stability

React also provides tools such as:

React.memo
useMemo
useCallback

These tools help prevent unnecessary renders by stabilizing props and avoiding repeated calculations. However, they should be used carefully, because excessive memoization can add complexity without improving performance.

Rendering Large Lists

When working with very large lists, rendering every element can become expensive. In such cases, techniques like virtualization can significantly improve performance by rendering only the visible portion of the list.

Libraries such as:

react-window
react-virtuoso

allow React applications to handle thousands of rows efficiently.

Measure Before Optimizing

Performance optimization should always begin with measurement rather than assumptions. Tools like the React DevTools Profiler help identify which components render, why they render, and how long they take.

Without profiling, optimization often becomes guesswork.

Read the Full Article

This post only provides a brief overview of the topic.

The full article explains in detail:

• React re-render mechanics
• architectural performance principles
• Context API performance pitfalls
• referential stability problems
• real-world optimization strategies for large React applications

Read the complete guide here:
https://aspnetzero.com/blog/react-performance-and-preventing-unnecessary-renders

Top comments (0)