Discover how React saves you from tedious, error-prone manual page updates!
Why Re-rendering behavior in React? - Purpose & Use Cases
Imagine you have a webpage showing a list of items and a counter. Every time you update the counter, you manually change the text in the HTML for both the counter and the list.
Manually updating each part of the page is slow and easy to mess up. You might forget to update some parts or accidentally erase others. It's hard to keep track of what changed and fix bugs.
React's re-rendering behavior automatically updates only the parts of the page that need to change when data updates. This keeps your UI in sync with your data without extra work.
document.getElementById('counter').innerText = count; document.getElementById('list').innerHTML = items.join(', ');
function Counter({count, items}) {
return <>
<p>{count}</p>
<ul>{items.map(item => <li key={item}>{item}</li>)}</ul>
</>;
}You can build dynamic, interactive apps that update smoothly and correctly without extra manual DOM work.
Think of a shopping cart that updates the total price and item list instantly when you add or remove products, without refreshing the whole page.
Manual DOM updates are slow and error-prone.
React re-renders only what changes automatically.
This makes UI updates easier, faster, and more reliable.
Practice
Solution
Step 1: Understand React's re-render triggers
React re-renders a component when its state or props change, not just any variable or style change.Step 2: Identify the correct trigger
UsingsetStateor the setter fromuseStateupdates state and triggers re-rendering.Final Answer:
When the component's state changes using a state setter likesetStateoruseStatesetter -> Option BQuick Check:
State change triggers re-render = B [OK]
- Thinking local variable changes cause re-render
- Assuming style changes trigger re-render
- Believing window resize triggers component re-render
Solution
Step 1: Recall state update syntax in functional components
In React functional components, state is updated using the setter function returned byuseState, e.g.,setCount.Step 2: Identify the correct syntax
Direct assignment likethis.state.count = 5orcount = 5does not trigger re-render. Only callingsetCount(5)updates state properly.Final Answer:
setCount(5); -> Option DQuick Check:
Use setter function to update state = C [OK]
- Trying to assign state directly
- Using undefined functions like updateState
- Confusing class component syntax with functional
function Counter() {
const [count, setCount] = React.useState(0);
console.log('Render:', count);
return (
<button onClick={() => setCount(count + 1)}>Count: {count}</button>
);
}What will be logged to the console after clicking the button three times?
Solution
Step 1: Understand initial render and state updates
Initially, the component renders with count = 0 and logs 'Render: 0'. Each button click callssetCount(count + 1), updating state and causing re-render.Step 2: Trace each click and render
After first click, count becomes 1 and logs 'Render: 1'. Second click updates count to 2 and logs 'Render: 2'. Third click updates count to 3 and logs 'Render: 3'.Final Answer:
Render: 0, Render: 1, Render: 2, Render: 3 -> Option AQuick Check:
Each state update triggers render log = A [OK]
- Ignoring initial render log
- Assuming only last render logs
- Thinking state updates batch without intermediate renders
function Example() {
const [value, setValue] = React.useState(0);
function update() {
value = value + 1;
}
return <button onClick={update}>Value: {value}</button>;
}Why does clicking the button NOT update the displayed value?
Solution
Step 1: Identify how state updates trigger re-render
React re-renders when state is updated via the setter function, not by direct variable assignment.Step 2: Analyze the update function
The function assignsvalue = value + 1directly, which changes a local variable but does not update React state or cause re-render.Final Answer:
Because direct assignment tovaluedoes not update state or trigger re-render -> Option AQuick Check:
State must be updated via setter to re-render = A [OK]
- Thinking direct assignment updates state
- Assuming event handler is not called
- Believing React forbids state updates in handlers
count to a child component. The child uses React.memo to avoid unnecessary re-renders. Which of these changes will cause the child to re-render?Solution
Step 1: Understand React.memo behavior
React.memoprevents re-render if props are shallowly equal. If props change, it re-renders.Step 2: Analyze each option
A: Samecountmeans props equal, no re-render.
B: Local variable change does not update state or props, no re-render.
C: Unrelated state change in parent does not affect child's props ifcountsame.
D: Newcountvalue means props changed, triggers re-render.Final Answer:
The parent passes a newcountvalue different from before -> Option CQuick Check:
Prop change triggers React.memo re-render = D [OK]
- Thinking parent re-render always triggers child re-render
- Ignoring prop equality in React.memo
- Thinking local variable changes cause re-render
Ready to prove it?
Pass the test and this pattern moves unknown
