0
0
Reactframework~10 mins

useState hook introduction in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - useState hook introduction
Component renders
useState called
State variable created with initial value
Component JSX uses state value
User triggers event
setState called with new value
React schedules re-render
Component re-renders with updated state
UI updates to show new state
The component renders and calls useState to create a state variable. When setState is called, React re-renders the component with the updated state, updating the UI.
Execution Sample
React
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
A simple counter button that shows a number and increases it by 1 each time you click.
Execution Table
StepActionState BeforeState AfterRendered Output
1Component renders first timecount: undefinedcount: 0<button>0</button>
2User clicks buttoncount: 0count: 0<button>0</button> (before re-render)
3setCount called with count + 1count: 0count: 1Scheduled re-render
4Component re-renderscount: 1count: 1<button>1</button>
5User clicks button againcount: 1count: 1<button>1</button> (before re-render)
6setCount called with count + 1count: 1count: 2Scheduled re-render
7Component re-renderscount: 2count: 2<button>2</button>
8No more clickscount: 2count: 2No change, UI shows 2
💡 Execution stops when user stops clicking; state remains at last updated value.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6After Step 7Final
countundefined011222
Key Moments - 3 Insights
Why does the component re-render after calling setCount?
Calling setCount schedules React to re-render the component with the new state value, as shown between steps 3 and 4 in the execution_table.
Why does the displayed number update only after re-render?
The UI updates only after React re-renders the component with the updated state, so the button shows the new count at steps 4 and 7.
Is the state variable 'count' changed immediately when setCount is called?
No, setCount schedules the update; the state variable changes on the next render, visible in the variable_tracker after re-render steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the value of 'count' and what is shown on the button?
Acount is 1, button shows 1
Bcount is 0, button shows 0
Ccount is 1, button shows 0
Dcount is 0, button shows 1
💡 Hint
Check the 'State After' and 'Rendered Output' columns at step 4 in the execution_table.
At which step does React schedule a re-render after the user clicks the button?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Scheduled re-render' in the 'Rendered Output' column in the execution_table.
If the initial useState value was 5 instead of 0, what would be the button label at step 1?
A0
B1
C5
DUndefined
💡 Hint
Refer to the variable_tracker 'After Step 1' value and initial useState value.
Concept Snapshot
useState hook creates a state variable and a setter function.
Syntax: const [state, setState] = useState(initialValue);
Calling setState(newValue) schedules a re-render.
Component re-renders with updated state.
UI updates to reflect new state value.
Full Transcript
The useState hook in React lets you add state to a functional component. When the component first renders, useState sets the state variable to the initial value. The component shows this value in the UI. When the user triggers an event, like clicking a button, the setter function updates the state. React then re-renders the component with the new state value, updating the UI. This process repeats each time the state changes, keeping the UI in sync with the state.