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, let's 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 (
<section>
<button
className='btn'
onClick={() => setCount(count + 1)}
style={{ marginBottom: '1rem' }}
>
count {count}
</button>
<List people={people} />
</section>
);
};
export default App;
In this setup, every time the count state changes, the entire App component (including the List component) re-renders. This is not efficient.
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 (
<section>
<Counter />
<List people={people} />
</section>
);
};
export default App;
By moving the counter button to a separate Counter component, we ensure that only the Counter component re-renders when the count changes, instead of the entire App component.
Updated Counter Component:
import { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState(0);
return (
<button
className='btn'
onClick={() => setCount(count + 1)}
style={{ marginBottom: '1rem' }}
>
count {count}
</button>
);
};
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 (like Person).
Profiler Settings:
Below are screenshots of the React Profiler settings with the re-rendered component highlighted:
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.






Top comments (1)
Refactoring to Reduce Re-renders on Input Change
In the initial code, every time the user types into the input field, the entire component re-renders, including the
Listcomponent. This happens becausesetPeopleis being called when a new person is added, which causes the entire component (including the form) to re-render. To optimize this, we can move the form into its own component so that only the form re-renders when the input changes, not the entireAppcomponent.Original Code (With Re-renders on Input Change):
Problem:
Every time the user types, the
Appcomponent re-renders, including theListcomponent, which is unnecessary.Refactored Code: Moving the Form to a Separate Component
To fix this, we can move the form into its own
AddPersonFormcomponent. This way, only the form will re-render when the input changes, not the entire app or the list of people.New
AddPersonFormComponent:In this refactor, the
AddPersonFormcomponent handles the form input and submission. It only re-renders when thenamestate changes, not the entireApp.Updated
AppComponent:Key Changes:
AddPersonFormcomponent.addPersonfunction, which updates the people list, is passed from theAppcomponent to theAddPersonFormas a prop. This ensures the list is updated when a new person is added, but the form itself doesn't cause unnecessary re-renders of theListcomponent.Benefits:
Listcomponent remains unaffected by those changes.