In React, a component may re-render in the following cases:
When the Component’s State or Props Change
React automatically re-renders a component whenever its state or props change. This ensures that the component reflects the latest data.When the Parent Component Re-renders
Even if the state or props of a child component remain unchanged, the child will re-render if its parent re-renders. This is the default behavior in React.
Tip: To optimize re-renders, you can use React.memo for functional components or shouldComponentUpdate for class components to prevent unnecessary re-renders when props haven't changed.
Credits: John Smilga's course
Top comments (2)
Optimizing Re-Renders by Moving State
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
countstate. By moving this state change to a separate component, we can prevent the entire parent from re-rendering.Before (with performance issue):
In this setup, every time the
countstate changes, the entireAppcomponent (including theListcomponent) re-renders. This is not efficient.Solution: Move the counter button to a separate
CountercomponentBy moving the counter button to a separate
Countercomponent, we ensure that only theCountercomponent re-renders when the count changes, instead of the entireAppcomponent.Updated
CounterComponent:The React Profiler
In the React Profiler, you’ll see that only the
Countercomponent re-renders when the button is clicked, not theListcomponent or its child components (likePerson).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.
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.