State lets React remember information and update the screen when that information changes. When state changes, React redraws the part of the screen that uses that state.
State re-render behavior in React
const [state, setState] = useState(initialValue); // To update state: setState(newValue);
Calling setState tells React to update the component.
React re-renders only the component that owns the state and its children.
const [count, setCount] = useState(0); setCount(count + 1);
const [visible, setVisible] = useState(false); setVisible(true);
const [text, setText] = useState('Hello'); setText('Hi there!');
This component shows a number that starts at 0. Each time you click the button, the number increases by 1. React re-renders the component to show the new number.
import React, { useState } from 'react'; export default function ClickCounter() { const [count, setCount] = useState(0); return ( <main> <h1>Clicked {count} times</h1> <button onClick={() => setCount(count + 1)} aria-label="Increase count"> Click me </button> </main> ); }
React batches multiple state updates inside event handlers for better performance.
Updating state with the same value does not cause a re-render.
Use functional updates (setState(prev => newVal)) when new state depends on old state.
State changes tell React to redraw the component.
Only the component with changed state and its children update.
Use setState to update state and trigger re-render.
Practice
setState?Solution
Step 1: Understand what
UsingsetStatedoessetStatetells 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 AQuick Check:
State change triggers re-render = B [OK]
- Thinking React reloads the whole page
- Believing state changes don't affect functional components
- Assuming UI doesn't update automatically
Solution
Step 1: Recall how state is updated in functional components
Functional components use theuseStatehook, which returns a setter function likesetCount.Step 2: Identify the correct syntax
CallingsetCount(1)updates the state and triggers a re-render.Final Answer:
setCount(1); -> Option DQuick Check:
Use setter function from useState = D [OK]
- Trying to assign state directly like a variable
- Using this.state in functional components
- Using non-existent functions like updateState
function Counter() {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
console.log('Rendered with count:', count);
});
return setCount(count + 1)}>Click {count};
}What will be logged to the console after clicking the button twice?
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 AQuick Check:
State updates cause re-render and useEffect logs each time = A [OK]
- Thinking useEffect runs only once
- Assuming logs show initial state only
- Ignoring that state updates cause re-renders
function Example() {
const [value, setValue] = React.useState(0);
function update() {
value = value + 1;
}
return Increase;
}Solution
Step 1: Identify how state is updated
State variables from useState are read-only. Direct assignment likevalue = value + 1does not update React state.Step 2: Understand correct state update method
State must be updated using the setter functionsetValueto trigger re-render.Final Answer:
Directly assigning to state variable does not update state or trigger re-render. -> Option BQuick Check:
State updates require setter function = C [OK]
- Assigning state variable directly
- Confusing event handlers like onChange vs onClick
- Thinking useState needs two arguments
setParentState. Which components re-render when the parent's state changes?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 CQuick Check:
Parent state change re-renders parent and children = A [OK]
- Thinking child won't re-render automatically
- Assuming only child re-renders
- Believing no re-render happens without explicit calls
Ready to prove it?
Pass the test and this pattern moves unknown
