0
0
Reactframework~10 mins

Updating phase in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Updating phase
Event or State Change
React schedules update
Component re-renders
JSX evaluated with new state/props
React compares new and old DOM (diff)
DOM updates applied
Browser shows updated UI
When state or props change, React re-renders the component, compares the new output with the old, and updates the browser view.
Execution Sample
React
import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
A button that shows a number and increases it by 1 each time you click.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Initial rendercount=0Render componentcount=0Counter componentButton shows '0'
2Click buttoncount=0setCount(count+1)count=1Counter componentButton updates to '1'
3Re-render after state updatecount=1Render componentcount=1Counter componentButton shows '1'
4Click buttoncount=1setCount(count+1)count=2Counter componentButton updates to '2'
5Re-render after state updatecount=2Render componentcount=2Counter componentButton shows '2'
6No further clickscount=2No actioncount=2No re-renderNo DOM change
💡 No more events trigger updates, so React stops re-rendering.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
count011222
Key Moments - 3 Insights
Why does React re-render the component after setCount is called?
React schedules an update when state changes (see step 2 in execution_table). This triggers a re-render to update the UI.
Does React re-render the whole page when state changes?
No, React only re-renders the component where state or props changed (see 'What Re-renders' column). It updates only the necessary DOM parts.
Why does the DOM update only after React compares new and old output?
React uses a diffing process to update only what changed for efficiency (see concept_flow). This avoids unnecessary DOM changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after step 4?
A1
B0
C2
DUndefined
💡 Hint
Check the 'State After' column at step 4 in execution_table.
At which step does React apply the DOM update to show '1' on the button?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the 'DOM Change' column showing button updates to '1'.
If the button is clicked again after step 5, what will happen next?
Acount becomes 3 and component re-renders
Bcount stays 2 and no re-render
Ccount resets to 0
DComponent unmounts
💡 Hint
Refer to the pattern of state updates and re-renders in execution_table steps 2-5.
Concept Snapshot
React Updating Phase:
- Triggered by state or props change
- React schedules re-render
- Component function runs again
- JSX output compared to previous
- Only changed DOM parts updated
- Browser shows new UI
- Efficient and fast updates
Full Transcript
The React updating phase starts when an event or state change happens, like clicking a button. React schedules an update and re-renders the component by running its function again with new state or props. It compares the new JSX output with the old one to find differences. Then React updates only the changed parts of the DOM. This process repeats for each state change, making the UI update smoothly and efficiently without reloading the whole page.