📖 Scenario: You are building a simple React component that shows a counter and a button to increase it. You want to understand how React re-renders the component when the state changes.
🎯 Goal: Create a React functional component that uses useState to hold a counter value. Add a button that increases the counter when clicked. Observe how the component re-renders when the state changes.
📋 What You'll Learn
Create a React functional component named Counter
Use useState to create a state variable count initialized to 0
Add a button with an onClick handler that increases count by 1
Display the current count value inside a <p> tag
💡 Why This Matters
🌍 Real World
Understanding re-rendering helps you build efficient React apps that update the UI correctly when data changes.
💼 Career
React developers must know how state changes cause re-renders to optimize performance and avoid bugs.
Progress0 / 4 steps
1
Set up the Counter component with initial state
Create a React functional component called Counter. Inside it, use useState to create a state variable named count initialized to 0.
React
Hint
Remember to import useState from React and call it inside your component to create state.
2
Add a button to increase the count
Inside the Counter component, add a <button> element with an onClick handler that calls setCount(count + 1) to increase the count by 1.
React
Hint
Use an arrow function inside onClick to call setCount(count + 1).
3
Display the current count value
Modify the Counter component to display the current count inside a <p> tag above the button.
React
Hint
Use a React fragment <></> to wrap multiple elements.
4
Add a console log to observe re-rendering
Inside the Counter component function body, add console.log('Counter rendered') to see when the component re-renders.
React
Hint
Place the console.log inside the component function but before the return statement.
Practice
(1/5)
1. Which of the following causes a React component to re-render?
easy
A. When a variable inside the component function changes but state does not
B. When the component's state changes using a state setter like setState or useState setter
C. When the component's CSS styles change
D. When the browser window is resized
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
Using setState or the setter from useState updates state and triggers re-rendering.
Final Answer:
When the component's state changes using a state setter like setState or useState setter -> Option B
Quick Check:
State change triggers re-render = B [OK]
Hint: State or props change triggers re-rendering in React [OK]
What will be logged to the console after clicking the button three times?
medium
A. Render: 0, Render: 1, Render: 2, Render: 3
B. Render: 0, Render: 1
C. Render: 3 only
D. Render: 1, Render: 2, Render: 3
Solution
Step 1: Understand initial render and state updates
Initially, the component renders with count = 0 and logs 'Render: 0'. Each button click calls setCount(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'.
Hint: Each state update triggers a new render and console log [OK]
Common Mistakes:
Ignoring initial render log
Assuming only last render logs
Thinking state updates batch without intermediate renders
4. Given this React component:
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?
medium
A. Because direct assignment to value does not update state or trigger re-render
B. Because the update function is not called on click
C. Because the initial state is zero and never changes
D. Because React does not allow state updates inside event handlers
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 assigns value = value + 1 directly, which changes a local variable but does not update React state or cause re-render.
Final Answer:
Because direct assignment to value does not update state or trigger re-render -> Option A
Quick Check:
State must be updated via setter to re-render = A [OK]
Hint: Always use state setter to update state, not direct assignment [OK]
Common Mistakes:
Thinking direct assignment updates state
Assuming event handler is not called
Believing React forbids state updates in handlers
5. You have a parent component passing a prop 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?
hard
A. The parent re-renders but passes the same count value as before
B. A variable inside the child component function changes but state does not
C. The parent passes a new count value different from before
D. The parent changes an unrelated state but count stays the same
Solution
Step 1: Understand React.memo behavior
React.memo prevents re-render if props are shallowly equal. If props change, it re-renders.
Step 2: Analyze each option
A: Same count means 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 if count same. D: New count value means props changed, triggers re-render.
Final Answer:
The parent passes a new count value different from before -> Option C
Quick Check:
Prop change triggers React.memo re-render = D [OK]
Hint: React.memo re-renders only if props change shallowly [OK]