0
0
Reactframework~10 mins

Controlled components in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Controlled components
Render Component
Input element value set from state
User types in input
onChange event triggers handler
Handler updates state with new input
Component re-renders with updated state
Back to Input element value set from state
The component renders an input whose value is controlled by React state. User input triggers an event that updates the state, causing re-render with new value.
Execution Sample
React
function ControlledInput() {
  const [text, setText] = React.useState('');
  return <input value={text} onChange={e => setText(e.target.value)} />;
}
A React component with an input field controlled by state, updating on user typing.
Execution Table
StepActionState BeforeEventState AfterRendered Input Value
1Initial rendertext = ''nonetext = '''' (empty)
2User types 'H'text = ''onChange with 'H'text = 'H''H'
3User types 'e'text = 'H'onChange with 'He'text = 'He''He'
4User types 'l'text = 'He'onChange with 'Hel'text = 'Hel''Hel'
5User types 'l'text = 'Hel'onChange with 'Hell'text = 'Hell''Hell'
6User types 'o'text = 'Hell'onChange with 'Hello'text = 'Hello''Hello'
7No more inputtext = 'Hello'nonetext = 'Hello''Hello'
💡 User stops typing; state remains stable with final input value.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
text'''''H''He''Hel''Hell''Hello''Hello'
Key Moments - 3 Insights
Why does the input value not change unless state updates?
Because the input's value is set from the state variable 'text'. Only when 'text' changes on onChange does the input update, as shown in steps 2-6.
What happens if we remove the onChange handler?
The input becomes read-only because its value is controlled by state but state never updates, so the input stays stuck at the initial value (step 1).
Why do we need to call setText inside onChange?
Calling setText updates the state with the new input value, triggering a re-render that updates the input's displayed value, as seen from step 2 onward.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'text' after step 4?
A'He'
B'Hel'
C'Hell'
D'Hello'
💡 Hint
Check the 'State After' column at step 4 in the execution table.
At which step does the input value first become 'Hello'?
AStep 7
BStep 5
CStep 6
DStep 4
💡 Hint
Look at the 'Rendered Input Value' column to find when 'Hello' appears.
If the onChange handler did not call setText, what would happen to the input value?
AIt would stay empty and never change
BIt would update normally as user types
CIt would show the last typed character only
DIt would cause an error
💡 Hint
Refer to the key moment about removing the onChange handler.
Concept Snapshot
Controlled Components in React:
- Input value is set from state.
- onChange updates state with user input.
- State change triggers re-render.
- Input reflects latest state value.
- Without onChange, input is read-only.
Full Transcript
This visual trace shows how a controlled component in React works. Initially, the input value is empty because the state variable 'text' is empty. When the user types a character, the onChange event fires, calling setText with the new input value. This updates the state, causing React to re-render the component with the updated 'text'. The input's value attribute reflects this new state, so the displayed text updates. This cycle repeats for each character typed. If the onChange handler is missing, the input value never changes because the state never updates, making the input read-only. Controlled components keep React state as the single source of truth for form inputs.