Optimizing Re-Renders by Moving State

Published: (December 30, 2025 at 09:58 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Introduction

One way to improve performance and reduce unnecessary re‑renders is by lowering the state, especially if it only affects a specific part of the component tree.

For example, consider a parent component that has a button to change a count state. By moving this state change to a separate component, we can prevent the entire parent from re‑rendering.

Before (with performance issue)

import { useState } from 'react';
import { data } from '../../../../data';
import List from './List';

const App = () => {
  const [people, setPeople] = useState(data);
  const [count, setCount] = useState(0);

  return (
    
       setCount(count + 1)}
        style={{ marginBottom: '1rem' }}
      >
        count {count}
      
      
    
  );
};
export default App;

In this setup, every time the count state changes, the entire App component (including the List component) re‑renders, which is inefficient.

Profiler Highlight

Solution: Move the counter button to a separate Counter component

import { useState } from 'react';
import { data } from '../../../../data';
import List from './List';
import Counter from './Counter';

const App = () => {
  const [people, setPeople] = useState(data);

  return (
    
      
      
    
  );
};
export default App;

By extracting the counter button into its own Counter component, only that component re‑renders when the count changes, leaving the rest of the UI untouched.

Updated Counter Component

import { useState } from 'react';

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

  return (
     setCount(count + 1)}
      style={{ marginBottom: '1rem' }}
    >
      count {count}
    
  );
};

export default Counter;

The React Profiler

In the React Profiler, you’ll see that only the Counter component re‑renders when the button is clicked, not the List component or its child components (e.g., Person).

Profiler Screenshots

Profiler Settings 1

Profiler Settings 2

Profiler Settings 3

Profiler Settings 4

Profiler Settings 5

Summary

By separating the state that affects specific UI elements (like the counter button) into its own component, we can reduce unnecessary re‑renders and improve performance.

Back to Blog

Related posts

Read more »