What happens in React when you update a component's state using useState?
React schedules a re-render of that component. The component function runs again, and React updates the UI to reflect the new state.
Click to reveal answer
intermediate
Does React re-render a component if the state value is set to the same value it already has?
No. React compares the new state with the current state using Object.is. If they are the same, React skips the re-render to improve performance.
Click to reveal answer
beginner
Why should you avoid directly mutating state in React?
Direct mutation does not change the state reference, so React may not detect a change and skip re-rendering. Always use the state setter function to update state immutably.
Click to reveal answer
intermediate
How does React batch multiple state updates inside event handlers?
React groups multiple state updates in event handlers into one re-render to improve performance, so the component only re-renders once after all updates.
Click to reveal answer
beginner
What is the role of useEffect in relation to state changes and re-rendering?
useEffect runs after the component renders. It can react to state changes to perform side effects like fetching data or updating the DOM outside React.
Click to reveal answer
What triggers a React component to re-render?
AUpdating the component's state using the setter function
BChanging a variable outside the component
CCalling a function inside the component without state change
DUpdating a CSS file
✗ Incorrect
Only updating the component's state with the setter function triggers React to re-render that component.
If you call the state setter with the same value as current state, what happens?
AReact skips the re-render
BReact re-renders the component anyway
CReact throws an error
DReact resets the state to initial value
✗ Incorrect
React compares the new and current state and skips re-render if they are the same to optimize performance.
Why is it important to avoid mutating state directly?
AIt causes React to re-render twice
BReact may not detect the change and skip re-render
CIt makes the app slower
DIt causes syntax errors
✗ Incorrect
Direct mutation does not change the state reference, so React may not detect the change and skip re-rendering.
How does React handle multiple state updates inside an event handler?
AIt re-renders after each update
BIt ignores all but the last update
CIt crashes the app
DIt batches updates and re-renders once
✗ Incorrect
React batches multiple state updates inside event handlers to re-render only once for better performance.
When does useEffect run in relation to state changes?
ABefore the component renders
BDuring the render
CAfter the component renders
DOnly on initial load
✗ Incorrect
useEffect runs after the component renders and can respond to state changes to perform side effects.
Explain how React decides when to re-render a component after a state update.
Think about how React checks if the state really changed before re-rendering.
You got /4 concepts.
Describe how React batches multiple state updates and why this is useful.
Imagine updating your shopping list several times quickly and React only updating the screen once.
You got /4 concepts.
Practice
(1/5)
1. What happens in React when you update a component's state using setState?
easy
A. React re-renders that component and its children to update the UI.
B. React reloads the entire page to reflect the changes.
C. React ignores the change unless the component is a class component.
D. React only updates the state internally without changing the UI.
Solution
Step 1: Understand what setState does
Using setState tells React that the component's state has changed and it needs to update the UI.
Step 2: Know React's rendering behavior
React re-renders the component where state changed and all its child components to reflect the new state.
Final Answer:
React re-renders that component and its children to update the UI. -> Option A
Quick Check:
State change triggers re-render = B [OK]
Hint: State change triggers re-render of component and children [OK]
Common Mistakes:
Thinking React reloads the whole page
Believing state changes don't affect functional components
Assuming UI doesn't update automatically
2. Which of the following is the correct way to update state in a React functional component?
easy
A. this.state.count = 1;
B. updateState({count: 1});
C. count = 1;
D. setCount(1);
Solution
Step 1: Recall how state is updated in functional components
Functional components use the useState hook, which returns a setter function like setCount.
Step 2: Identify the correct syntax
Calling setCount(1) updates the state and triggers a re-render.
Final Answer:
setCount(1); -> Option D
Quick Check:
Use setter function from useState = D [OK]
Hint: Use the setter function from useState to update state [OK]
What will be logged to the console after clicking the button twice?
medium
A. 'Rendered with count: 1' then 'Rendered with count: 2'
B. 'Rendered with count: 0' only once
C. 'Rendered with count: 2' only once
D. No logs will appear
Solution
Step 1: Understand useEffect without dependencies
Without a dependency array, useEffect runs after every render.
Step 2: Trace state changes on clicks
Initial count is 0. Clicking once sets count to 1, triggering a render and logging 'Rendered with count: 1'. Clicking again sets count to 2, triggering another render and logging 'Rendered with count: 2'.
Final Answer:
'Rendered with count: 1' then 'Rendered with count: 2' -> Option A
Quick Check:
State updates cause re-render and useEffect logs each time = A [OK]
Hint: useEffect without deps runs after every render [OK]
Common Mistakes:
Thinking useEffect runs only once
Assuming logs show initial state only
Ignoring that state updates cause re-renders
4. What is wrong with this React code snippet?
function Example() {
const [value, setValue] = React.useState(0);
function update() {
value = value + 1;
}
return Increase;
}
medium
A. useState hook is used incorrectly; it needs two arguments.
B. Directly assigning to state variable does not update state or trigger re-render.
C. The function update should be async to update state.
D. The button should use onChange instead of onClick.
Solution
Step 1: Identify how state is updated
State variables from useState are read-only. Direct assignment like value = value + 1 does not update React state.
Step 2: Understand correct state update method
State must be updated using the setter function setValue to trigger re-render.
Final Answer:
Directly assigning to state variable does not update state or trigger re-render. -> Option B
Quick Check:
State updates require setter function = C [OK]
Hint: Never assign state variable directly; use setter function [OK]
Common Mistakes:
Assigning state variable directly
Confusing event handlers like onChange vs onClick
Thinking useState needs two arguments
5. You have a parent component that renders a child component. The parent updates its state with setParentState. Which components re-render when the parent's state changes?
hard
A. Only the child component re-renders; the parent does not.
B. Only the parent component re-renders; the child does not.
C. Both the parent and child components re-render.
D. Neither component re-renders automatically.
Solution
Step 1: Understand React's re-render on state change
When a parent component's state changes, React re-renders that parent component.
Step 2: Know child component rendering behavior
Since the child is rendered inside the parent, React also re-renders the child unless memoization or other optimizations are used.
Final Answer:
Both the parent and child components re-render. -> Option C
Quick Check:
Parent state change re-renders parent and children = A [OK]
Hint: Parent state change re-renders parent and children by default [OK]
Common Mistakes:
Thinking child won't re-render automatically
Assuming only child re-renders
Believing no re-render happens without explicit calls
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.