Bird
Raised Fist0
Reactframework~8 mins

Re-rendering behavior in React - Performance & Optimization

Choose your learning style10 modes available
Performance: Re-rendering behavior
MEDIUM IMPACT
This affects how often React updates the UI, impacting interaction responsiveness and CPU usage.
Updating a component state causing all child components to re-render unnecessarily
React
const Child = React.memo(function Child() {
  console.log('Child rendered');
  return <div>Child component</div>;
});

function Parent() {
  const [count, setCount] = React.useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child />
    </div>
  );
}
React.memo prevents Child from re-rendering unless its props change, reducing unnecessary renders.
πŸ“ˆ Performance GainReduces re-renders of Child from every state update to zero unless props change.
Updating a component state causing all child components to re-render unnecessarily
React
function Parent() {
  const [count, setCount] = React.useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child />
    </div>
  );
}

function Child() {
  console.log('Child rendered');
  return <div>Child component</div>;
}
Child component re-renders every time Parent updates state, even if Child does not depend on that state.
πŸ“‰ Performance CostTriggers 1 re-render per state update, increasing CPU usage and slowing interaction.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Parent state update triggers all children re-renderMany virtual DOM diffsMultiple reflows if DOM changesHigher paint cost due to updates[X] Bad
Using React.memo to prevent unnecessary child re-rendersMinimal virtual DOM diffsSingle or no reflows if no DOM changesLower paint cost[OK] Good
Passing new inline functions as props causing re-rendersVirtual DOM diffs triggered by prop changesReflows triggered if DOM updatesIncreased paint cost[X] Bad
Memoizing functions with useCallback to stabilize propsReduced virtual DOM diffsFewer reflowsReduced paint cost[OK] Good
Rendering Pipeline
React re-rendering triggers virtual DOM diffing, which leads to real DOM updates if changes are detected. Excessive re-renders increase CPU work and can delay painting.
β†’JavaScript Execution
β†’Virtual DOM Diffing
β†’Layout
β†’Paint
⚠️ BottleneckJavaScript Execution and Virtual DOM Diffing
Core Web Vital Affected
INP
This affects how often React updates the UI, impacting interaction responsiveness and CPU usage.
Optimization Tips
1Wrap pure components with React.memo to avoid re-rendering when props don’t change.
2Use useCallback to memoize functions passed as props to prevent new references each render.
3Avoid creating new objects or arrays inline in JSX props to reduce re-renders.
Performance Quiz - 3 Questions
Test your performance knowledge
What causes a React component wrapped in React.memo to still re-render unnecessarily?
ANot using keys in lists
BUsing useState inside the component
CPassing new inline functions or objects as props each render
DHaving too many child components
DevTools: React Developer Tools Profiler
How to check: Open React DevTools, go to Profiler tab, record interactions, and observe which components re-render and how long they take.
What to look for: Look for components that re-render frequently without prop or state changes, indicating unnecessary re-renders.

Practice

(1/5)
1. Which of the following causes a React component to re-render?
easy
A. When a variable inside the component function changes but state does not
B. When the component's state changes using a state setter like setState or useState setter
C. When the component's CSS styles change
D. When the browser window is resized

Solution

  1. Step 1: Understand React's re-render triggers

    React re-renders a component when its state or props change, not just any variable or style change.
  2. Step 2: Identify the correct trigger

    Using setState or the setter from useState updates state and triggers re-rendering.
  3. Final Answer:

    When the component's state changes using a state setter like setState or useState setter -> Option B
  4. Quick Check:

    State change triggers re-render = B [OK]
Hint: State or props change triggers re-rendering in React [OK]
Common Mistakes:
  • Thinking local variable changes cause re-render
  • Assuming style changes trigger re-render
  • Believing window resize triggers component re-render
2. Which of the following is the correct way to update state and trigger a re-render in a React functional component?
easy
A. this.state.count = 5;
B. count = 5;
C. updateState(count = 5);
D. setCount(5);

Solution

  1. Step 1: Recall state update syntax in functional components

    In React functional components, state is updated using the setter function returned by useState, e.g., setCount.
  2. Step 2: Identify the correct syntax

    Direct assignment like this.state.count = 5 or count = 5 does not trigger re-render. Only calling setCount(5) updates state properly.
  3. Final Answer:

    setCount(5); -> Option D
  4. Quick Check:

    Use setter function to update state = C [OK]
Hint: Use setter from useState to update state, not direct assignment [OK]
Common Mistakes:
  • Trying to assign state directly
  • Using undefined functions like updateState
  • Confusing class component syntax with functional
3. Consider this React functional component:
function Counter() {
  const [count, setCount] = React.useState(0);
  console.log('Render:', count);
  return (
    <button onClick={() => setCount(count + 1)}>Count: {count}</button>
  );
}

What will be logged to the console after clicking the button three times?
medium
A. Render: 0, Render: 1, Render: 2, Render: 3
B. Render: 0, Render: 1
C. Render: 3 only
D. Render: 1, Render: 2, Render: 3

Solution

  1. Step 1: Understand initial render and state updates

    Initially, the component renders with count = 0 and logs 'Render: 0'. Each button click calls setCount(count + 1), updating state and causing re-render.
  2. Step 2: Trace each click and render

    After first click, count becomes 1 and logs 'Render: 1'. Second click updates count to 2 and logs 'Render: 2'. Third click updates count to 3 and logs 'Render: 3'.
  3. Final Answer:

    Render: 0, Render: 1, Render: 2, Render: 3 -> Option A
  4. Quick Check:

    Each state update triggers render log = A [OK]
Hint: Each state update triggers a new render and console log [OK]
Common Mistakes:
  • Ignoring initial render log
  • Assuming only last render logs
  • Thinking state updates batch without intermediate renders
4. Given this React component:
function Example() {
  const [value, setValue] = React.useState(0);
  function update() {
    value = value + 1;
  }
  return <button onClick={update}>Value: {value}</button>;
}

Why does clicking the button NOT update the displayed value?
medium
A. Because direct assignment to value does not update state or trigger re-render
B. Because the update function is not called on click
C. Because the initial state is zero and never changes
D. Because React does not allow state updates inside event handlers

Solution

  1. Step 1: Identify how state updates trigger re-render

    React re-renders when state is updated via the setter function, not by direct variable assignment.
  2. Step 2: Analyze the update function

    The function assigns value = value + 1 directly, which changes a local variable but does not update React state or cause re-render.
  3. Final Answer:

    Because direct assignment to value does not update state or trigger re-render -> Option A
  4. Quick Check:

    State must be updated via setter to re-render = A [OK]
Hint: Always use state setter to update state, not direct assignment [OK]
Common Mistakes:
  • Thinking direct assignment updates state
  • Assuming event handler is not called
  • Believing React forbids state updates in handlers
5. You have a parent component passing a prop count to a child component. The child uses React.memo to avoid unnecessary re-renders. Which of these changes will cause the child to re-render?
hard
A. The parent re-renders but passes the same count value as before
B. A variable inside the child component function changes but state does not
C. The parent passes a new count value different from before
D. The parent changes an unrelated state but count stays the same

Solution

  1. Step 1: Understand React.memo behavior

    React.memo prevents re-render if props are shallowly equal. If props change, it re-renders.
  2. Step 2: Analyze each option

    A: Same count means props equal, no re-render.
    B: Local variable change does not update state or props, no re-render.
    C: Unrelated state change in parent does not affect child's props if count same.
    D: New count value means props changed, triggers re-render.
  3. Final Answer:

    The parent passes a new count value different from before -> Option C
  4. Quick Check:

    Prop change triggers React.memo re-render = D [OK]
Hint: React.memo re-renders only if props change shallowly [OK]
Common Mistakes:
  • Thinking parent re-render always triggers child re-render
  • Ignoring prop equality in React.memo
  • Thinking local variable changes cause re-render
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard