Bird
Raised Fist0
Reactframework~10 mins

State re-render behavior in React - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - State re-render behavior
Initial Render
↓
Component Renders with initial state
↓
User triggers event
↓
setState called
↓
React schedules re-render
↓
Component re-renders with updated state
↓
DOM updates to reflect new state
↓
Wait for next event or interaction
This flow shows how React components render initially, then re-render when state changes, updating the UI accordingly.
Execution Sample
React
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>Count: {count}</button>
  );
}

export default Counter;
A simple button that shows a count and increases it by 1 each time you click.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Initial rendercount=0Render componentcount=0Whole Counter componentButton shows 'Count: 0'
2User clicks buttoncount=0setCount(count + 1)count=1Whole Counter componentButton updates to 'Count: 1'
3User clicks button againcount=1setCount(count + 1)count=2Whole Counter componentButton updates to 'Count: 2'
4No more clickscount=2No actioncount=2No re-renderDOM stays 'Count: 2'
💡 No further state changes, so no more re-renders.
Variable Tracker
VariableStartAfter 1After 2Final
count0122
Key Moments - 3 Insights
Why does the whole component re-render when only the count changes?
React re-renders the entire functional component when state changes to update the UI. The execution_table rows 2 and 3 show the whole Counter component re-rendering with the new count.
Does React update the DOM immediately after setState is called?
No, React schedules the update and batches changes. The DOM updates after the component re-renders, as shown in execution_table steps 2 and 3.
What happens if setState is called with the same value as current state?
React skips re-rendering because state didn't change. This is why in step 4, no re-render or DOM change happens.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'count' after step 2?
A0
B2
C1
DUndefined
💡 Hint
Check the 'State After' column in row 2 of the execution_table.
At which step does the DOM update to show 'Count: 2'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'DOM Change' column for when the button text changes to 'Count: 2'.
If setCount was called with the same value as current count, what would happen?
ANo re-render or DOM update
BComponent re-renders but DOM does not update
CComponent re-renders and DOM updates
DError occurs
💡 Hint
Refer to step 4 in execution_table where no action leads to no re-render.
Concept Snapshot
React state changes cause the whole functional component to re-render.
Use useState to hold state and setState to update it.
When state changes, React schedules a re-render.
The DOM updates after re-render to reflect new state.
If state doesn't change, React skips re-rendering.
This keeps UI and state in sync efficiently.
Full Transcript
This lesson shows how React components update when state changes. Initially, the Counter component renders with count zero. When the user clicks the button, setCount updates the count state. React then re-renders the entire component with the new count value. The DOM updates to show the new count on the button. If the user clicks again, the process repeats with the updated count. If setCount is called with the same value, React skips re-rendering to optimize performance. This flow ensures the UI always matches the current state.

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