Bird
Raised Fist0
Reactframework~8 mins

State re-render behavior in React - Performance & Optimization

Choose your learning style10 modes available
Performance: State re-render behavior
HIGH IMPACT
This affects how often React updates the UI, impacting interaction responsiveness and rendering speed.
Updating state frequently inside a component
React
function Counter() {
  const [count, setCount] = React.useState(0);
  function increment() {
    setCount(prev => prev + 2);
  }
  return <button onClick={increment}>{count}</button>;
}
Single functional state update chains correctly (+2), avoiding stale closure issues.
📈 Performance GainCorrect state (+2), single state update and one re-render.
Updating state frequently inside a component
React
function Counter() {
  const [count, setCount] = React.useState(0);
  function increment() {
    setCount(count + 1);
    setCount(count + 1);
  }
  return <button onClick={increment}>{count}</button>;
}
Multiple state updates capture the stale count from closure, resulting in incorrect state (+1 instead of intended +2).
📉 Performance CostIncorrect final count (+1 not +2), multiple queued updates batched into one re-render.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Multiple state updates per eventMultiple state queues, 1 virtual DOM diffSingle reflowNormal paint cost[X] Bad
Single functional state updateOne virtual DOM diffSingle reflowLower paint cost[OK] Good
State updates with same valueEquality check performedNo reflowsNo paint[X] Bad
Avoid state update if value unchangedNo virtual DOM diffNo reflowNo paint[OK] Good
Parent state update without memoAll children re-renderMultiple reflowsHigh paint cost[X] Bad
Memoized child componentsSelective re-renderMinimal reflowsReduced paint[OK] Good
Rendering Pipeline
When state updates, React schedules a re-render of the component. React then recalculates the virtual DOM, diffs it with the previous version, and updates the real DOM accordingly. This process affects style calculation, layout, paint, and composite stages depending on changes.
→Virtual DOM Diffing
→Style Calculation
→Layout
→Paint
→Composite
⚠️ BottleneckLayout and Paint stages are most expensive when many DOM nodes update due to state changes.
Core Web Vital Affected
INP
This affects how often React updates the UI, impacting interaction responsiveness and rendering speed.
Optimization Tips
1Batch multiple state updates to trigger a single re-render.
2Avoid calling setState with the same value to minimize overhead.
3Use React.memo to prevent child components from re-rendering when not needed.
Performance Quiz - 3 Questions
Test your performance knowledge
What happens when you call setState multiple times synchronously in React?
AReact triggers multiple re-renders, one per setState call
BReact batches updates and triggers a single re-render
CReact ignores all but the last setState call
DReact throws an error
DevTools: React DevTools and Chrome Performance panel
How to check: Use React DevTools Profiler to record interactions and see which components re-render. Use Chrome Performance panel to record and analyze layout and paint times during state updates.
What to look for: Look for unnecessary component re-renders in React DevTools and long layout or paint durations in Performance panel indicating expensive updates.

Practice

(1/5)
1. What happens in React when you update a component's state using setState?
easy
A. React re-renders that component and its children to update the UI.
B. React reloads the entire page to reflect the changes.
C. React ignores the change unless the component is a class component.
D. React only updates the state internally without changing the UI.

Solution

  1. Step 1: Understand what setState does

    Using setState tells React that the component's state has changed and it needs to update the UI.
  2. Step 2: Know React's rendering behavior

    React re-renders the component where state changed and all its child components to reflect the new state.
  3. Final Answer:

    React re-renders that component and its children to update the UI. -> Option A
  4. Quick Check:

    State change triggers re-render = B [OK]
Hint: State change triggers re-render of component and children [OK]
Common Mistakes:
  • Thinking React reloads the whole page
  • Believing state changes don't affect functional components
  • Assuming UI doesn't update automatically
2. Which of the following is the correct way to update state in a React functional component?
easy
A. this.state.count = 1;
B. updateState({count: 1});
C. count = 1;
D. setCount(1);

Solution

  1. Step 1: Recall how state is updated in functional components

    Functional components use the useState hook, which returns a setter function like setCount.
  2. Step 2: Identify the correct syntax

    Calling setCount(1) updates the state and triggers a re-render.
  3. Final Answer:

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

    Use setter function from useState = D [OK]
Hint: Use the setter function from useState to update state [OK]
Common Mistakes:
  • Trying to assign state directly like a variable
  • Using this.state in functional components
  • Using non-existent functions like updateState
3. Consider this React component code:
function Counter() {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    console.log('Rendered with count:', count);
  });
  return  setCount(count + 1)}>Click {count};
}

What will be logged to the console after clicking the button twice?
medium
A. 'Rendered with count: 1' then 'Rendered with count: 2'
B. 'Rendered with count: 0' only once
C. 'Rendered with count: 2' only once
D. No logs will appear

Solution

  1. Step 1: Understand useEffect without dependencies

    Without a dependency array, useEffect runs after every render.
  2. Step 2: Trace state changes on clicks

    Initial count is 0. Clicking once sets count to 1, triggering a render and logging 'Rendered with count: 1'. Clicking again sets count to 2, triggering another render and logging 'Rendered with count: 2'.
  3. Final Answer:

    'Rendered with count: 1' then 'Rendered with count: 2' -> Option A
  4. Quick Check:

    State updates cause re-render and useEffect logs each time = A [OK]
Hint: useEffect without deps runs after every render [OK]
Common Mistakes:
  • Thinking useEffect runs only once
  • Assuming logs show initial state only
  • Ignoring that state updates cause re-renders
4. What is wrong with this React code snippet?
function Example() {
  const [value, setValue] = React.useState(0);
  function update() {
    value = value + 1;
  }
  return Increase;
}
medium
A. useState hook is used incorrectly; it needs two arguments.
B. Directly assigning to state variable does not update state or trigger re-render.
C. The function update should be async to update state.
D. The button should use onChange instead of onClick.

Solution

  1. Step 1: Identify how state is updated

    State variables from useState are read-only. Direct assignment like value = value + 1 does not update React state.
  2. Step 2: Understand correct state update method

    State must be updated using the setter function setValue to trigger re-render.
  3. Final Answer:

    Directly assigning to state variable does not update state or trigger re-render. -> Option B
  4. Quick Check:

    State updates require setter function = C [OK]
Hint: Never assign state variable directly; use setter function [OK]
Common Mistakes:
  • Assigning state variable directly
  • Confusing event handlers like onChange vs onClick
  • Thinking useState needs two arguments
5. You have a parent component that renders a child component. The parent updates its state with setParentState. Which components re-render when the parent's state changes?
hard
A. Only the child component re-renders; the parent does not.
B. Only the parent component re-renders; the child does not.
C. Both the parent and child components re-render.
D. Neither component re-renders automatically.

Solution

  1. Step 1: Understand React's re-render on state change

    When a parent component's state changes, React re-renders that parent component.
  2. Step 2: Know child component rendering behavior

    Since the child is rendered inside the parent, React also re-renders the child unless memoization or other optimizations are used.
  3. Final Answer:

    Both the parent and child components re-render. -> Option C
  4. Quick Check:

    Parent state change re-renders parent and children = A [OK]
Hint: Parent state change re-renders parent and children by default [OK]
Common Mistakes:
  • Thinking child won't re-render automatically
  • Assuming only child re-renders
  • Believing no re-render happens without explicit calls
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