Understanding Re-Renders in React
A re-render occurs when React calls a component function again to determine what the user interface should look like. Re-rendering is a normal part of React and helps the application display updated data whenever necessary.
However, when an application contains many components, understanding why and when they render becomes important. It helps developers identify unnecessary work and improve application performance.
1. What Is Rendering?
Rendering is the process in which React executes a component to calculate the UI that should be displayed. For a function component, React calls its function and uses the returned JSX to determine the output.
A component may render when:
- Its state changes.
- Its parent component renders again.
- Its props change as part of an update.
- A context value it consumes changes.
2. How Does a Re-Render Work?
React may render a component without making any visible DOM changes. It compares the new result with the previous result and updates the DOM when needed.
3. Example: State Change Causes a Re-Render
In this example, the Counter component stores a number in state. Whenever the button is clicked, the state changes and React renders the component again to display the new number.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
console.log("Counter rendered");
return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
</div>
);
}
export default Counter;
Expected Output
Initial display: Count: 0
After one click: Count: 1
After two clicks: Count: 2
The console message "Counter rendered" appears when React executes the component. Development Strict Mode may cause additional render calls.
Explanation
- Initially, count is 0.
- The user clicks the Increase button.
- setCount updates the state to the next value.
- React renders the Counter component again.
- The heading displays the updated count.
4. Parent and Child Re-Renders
By default, when a parent component renders again, React may also render its child components. This can happen even if a child receives the same primitive prop values as before.
import { useState } from "react";
function Child({ name }) {
console.log("Child rendered");
return <h3>Hello, {name}</h3>;
}
function Parent() {
const [count, setCount] = useState(0);
return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>
Increase Count
</button>
<Child name="Samadhan" />
</div>
);
}
export default Parent;
Expected Behavior
Count: 0
Button: Increase Count
Hello, Samadhan
When the count changes, Parent renders again. Child may also render again, even though its name prop remains "Samadhan".
5. Re-Rendering Does Not Always Mean a Performance Problem
Re-rendering is essential for displaying updated data. Most simple components render quickly, so trying to prevent every render can make code more complicated without improving the user experience.
- Necessary re-renders keep the UI synchronized with state.
- Unnecessary re-renders may waste work in expensive components.
- React can avoid DOM updates when the rendered output has not changed.
- Performance should be measured before introducing optimizations.
6. Common Reasons for Re-Renders
State Updates
Calling a state setter with a different value can cause React to render the component again.
Parent Component Updates
When a parent renders, its child components can render again as part of the normal rendering process.
Context Updates
Components that consume a context value may render again when the context value they use changes.
Changed Props
New prop values can affect what a component displays and may require it to render again.
7. Practice Tasks
- Create a Counter component using useState.
- Add a console message inside a component function.
- Create a Parent component with one Child component.
- Change the parent's state and observe the console.
- Explain the difference between a re-render and a DOM update.
- List three common reasons for a component to render again.
Summary
Re-rendering is a normal part of React's rendering process. A component can render again because its state changes, its parent renders, its props change, or a context value it consumes changes. React compares the new rendered result with the previous one and updates the DOM when necessary. Re-renders are not always a performance problem, so developers should understand their causes and measure performance before applying optimizations. CIIT Training Institute recommends practicing with state and parent-child components to understand rendering behavior clearly.