Skip to main content

What happens when a component updates?

What "updating" a component means

An update is the lifecycle stage where React re-invokes the component to update its virtual representation (Virtual DOM) and synchronize the changes with the real DOM.


What triggers an update

A component re-renders when:

ReasonExample
State (state) changedsetCount(count + 1)
New props (props) arrivedThe parent passed new data
The parent component itself re-renderedEven if the props didn't change
Context (useContext) updatedThe provider's value changed

Update stages step by step

1. Data changes

React gets a signal that the data has changed:

javascript
setCount(count + 1);

This doesn't trigger an update immediately. React puts the update in a queue (batch) and decides when to re-render the component (especially in React 18 with concurrent rendering).


2. The component function is called again

React calls the component again (like an ordinary function) to compute what JSX it should now render.

javascript
function Counter() { console.log("Render"); const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>Counter: {count}</button>; }

Each call to the component = a new "render".


3. Comparing the Virtual DOM (diffing)

React compares the new virtual tree (the result of the new JSX) with the previous tree.

  • If elements differ -> React changes only the parts of the DOM that changed.
  • If they're the same -> the DOM is left untouched.

This makes updates fast and targeted, without a full page repaint.


4. Updating the real DOM

React applies only the necessary changes to the browser DOM.

For example: if only the text in a button changed, React will replace only the text, not recreate the whole button.


5. Running effects (useEffect, useLayoutEffect)

Once the DOM is updated, React runs the effects whose dependencies changed.

javascript
useEffect(() => { console.log("Update: count changed"); }, [count]);

If count changed, the effect will run again. Before that, React will call the cleanup function of the previous effect.


Example: the full update cycle

javascript
function Example() { const [count, setCount] = useState(0); useEffect(() => { console.log("Mount"); return () => console.log("Unmount"); }, []); useEffect(() => { console.log("Update: count =", count); return () => console.log("Cleanup before the next effect"); }, [count]); return ( <button onClick={() => setCount(c => c + 1)}> Counter: {count} </button> ); }

What happens:

javascript
Mount Update: count = 0 ← first effect run (button clicked) Cleanup before the next effect Update: count = 1 (button clicked) Cleanup before the next effect Update: count = 2

Difference between mounting and updating

BehaviorMountingUpdating
Is the component calledYes, for the first timeYes, again
Is the DOM createdFullyOnly the changes
useEffect(() => {...}, [])FiresDoesn't fire
useEffect(() => {...}, [deps])Fires after mountingOn dependency change
cleanup in useEffectNoneBefore the next effect call

What's usually done during an update

TaskExample
React to data changesuseEffect(() => {...}, [value])
Synchronize the DOM with the new stateApplying animations, scrolling
Update subscriptionsUnsubscribe first, then subscribe again
Recompute derived valuesuseMemo
Optimize re-rendersReact.memo, useCallback

Class component equivalents

StageClass methodFunctional equivalent
After updatecomponentDidUpdate(prevProps, prevState)useEffect(() => {...}, [deps])
Before update (rarely used)shouldComponentUpdate()React.memo / useMemo / useCallback

Important to remember

FeatureExplanation
The component function is called again on every updateBut state (useState) persists between renders
React itself optimizes DOM updatesIt changes only what's needed
You can't change the DOM directly in the function bodyOnly in effects (useEffect, useLayoutEffect)
useEffect runs after the DOM update (asynchronously)The UI updates first, then the effect runs
useLayoutEffect runs before the screen paintsFor synchronizing element sizes or positions

Summary

StageWhat React doesWhat the developer does
1. Data changeReceives new state or propsCalls setState / setCount
2. RenderCalls the component againReturns new JSX
3. DiffCompares the old and new Virtual DOMDoes nothing - React handles it
4. CommitUpdates the real DOMCan react in useEffect
5. EffectsRuns hooks whose dependencies changedPerforms side effects

Main idea:

During an update, React calls the component again, recreates the Virtual DOM, compares it with the previous one, updates only the necessary parts of the DOM, and runs the effects whose dependencies changed.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.