← Back to Chapters

Re-render Triggers

⚛️ Re-render Triggers

React Rendering Performance

? Quick Overview

A re-render happens when React decides that a component’s output may have changed and it needs to call the component again to compute the new UI.

  • React compares the previous Virtual DOM tree with the new one.
  • Only the parts of the real DOM that changed are updated.
  • Knowing what triggers re-renders helps you avoid unnecessary work and keep apps fast.

? Key Concepts: What Triggers a Re-render?

  • 1. State changes — any update using useState or this.setState causes a re-render.
  • 2. Props changes — when a component receives new or changed props from its parent.
  • 3. Context value changes — when a consumed context value is updated.
  • 4. Parent re-renders — children re-render when their parent re-renders (unless memoized).
  • 5. Force updates — calling forceUpdate() in class components.

? Theory & Syntax

React components are pure functions of props and state:

  • Whenever state changes → component is scheduled to re-render.
  • Whenever props change → component is scheduled to re-render.
  • When a parent re-renders → all its children are checked and usually re-render too.
  • React may batch multiple state updates into a single re-render for performance.

? Example 1 — State Change Trigger

Here, updating the count state triggers a re-render on every button click.

? View Code Example: State Change Trigger
// Counter re-renders every time its state (count) changes
import { useState } from "react";

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

console.log("Counter component re-rendered");

return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}

? What Happens When You Click?

  • setCount(count + 1) updates the state value.
  • React schedules a re-render of Counter.
  • The function runs again and logs "Counter component re-rendered".
  • The UI updates to show the new count.

? Example 2 — Prop Change Trigger

When the parent’s state changes, it passes a new prop to the child, which triggers the child’s re-render.

? View Code Example: Prop Change Trigger
// Child re-renders whenever the value prop coming from Parent changes
import { useState } from "react";

function Child({ value }) {
console.log("Child re-rendered");
return <p>Value: {value}</p>;
}

function Parent() {
const [num, setNum] = useState(0);

return (
<div>
<button onClick={() => setNum(num + 1)}>Update Parent</button>
<Child value={num} />
</div>
);
}

? Render Flow

  1. num is stored as state in Parent.
  2. Clicking the button updates num.
  3. Parent re-renders and passes a new value prop to Child.
  4. Child re-renders and logs "Child re-rendered" again.

⚙️ Avoiding Unnecessary Re-renders

If a component keeps re-rendering even when its inputs don’t change, you can optimize it using React.memo, useCallback, and useMemo.

  • ✅ Use React.memo() to memoize components that receive unchanged props.
  • ✅ Use useCallback() to avoid recreating functions on every render.
  • ✅ Use useMemo() to cache expensive calculations.
  • ✅ Split large components into smaller pieces.
  • ✅ Avoid setting the same state value repeatedly (e.g. setCount(count)).
? View Code Example: Using React.memo
// MemoizedChild only re-renders when its props actually change
const MemoizedChild = React.memo(function Child({ value }) {
console.log("MemoizedChild rendered");
return <p>Value: {value}</p>;
});

? Re-render vs Repaint

  • Re-render: React calls your component again, builds a new Virtual DOM tree, and compares it with the previous one.
  • Repaint: After React updates the real DOM, the browser updates pixels on the screen.

Not every React re-render leads to a heavy visual repaint — React and the browser both try to minimize expensive work.

? Tips & Best Practices

  • Group related state into a single update to avoid multiple re-renders.
  • Remember that React batches state updates inside event handlers for performance.
  • Use React.memo for pure components that depend only on their props.
  • Use React DevTools “Highlight updates” to see which components are re-rendering.
  • Watch logs like console.log("Component re-rendered") during development to understand render frequency.

? Try It Yourself

  1. Create a Parent and Child component. Log to the console when each one renders.
  2. Add state to the parent and update it on button click. Observe how both components re-render.
  3. Wrap the Child in React.memo() and confirm that it only re-renders when its props change.
  4. Introduce a callback prop (e.g. onClick) and memoize it using useCallback.
  5. Experiment with an expensive calculation wrapped in useMemo and see how it affects performance.

Goal: Understand exactly what triggers React re-renders and apply memoization techniques to prevent unnecessary renders.