The useState hook lets you add and keep track of changing data in a React component. It helps your app remember things like clicks or typed text.
useState hook introduction in React
const [state, setState] = useState(initialValue);state is the current value you want to remember.
setState is a function you call to change that value and update the screen.
const [count, setCount] = useState(0);
const [name, setName] = useState('');
const [isVisible, setIsVisible] = useState(true);
This simple component shows a number and a button. Each time you click the button, the number goes up by one. The useState hook keeps track of the number and updates the screen.
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return ( <main> <h1>Count: {count}</h1> <button onClick={() => setCount(count + 1)} aria-label="Increase count"> Increase </button> </main> ); } export default Counter;
Always call useState at the top level of your component, not inside loops or conditions.
Calling the setter function (like setCount) tells React to update the screen with the new value.
You can use any type of value in useState: numbers, text, true/false, or even objects.
useState lets your React component remember and change data.
It returns the current value and a function to update it.
Updating state causes React to refresh what the user sees.
Practice
useState hook do in a React component?Solution
Step 1: Understand the purpose of useState
TheuseStatehook is designed to let a React component keep track of data that can change.Step 2: Identify what useState does
It returns the current value and a function to update that value, enabling the component to remember and update data.Final Answer:
It allows the component to remember and update data over time. -> Option BQuick Check:
useState manages component data = A [OK]
- Thinking useState fetches data from APIs
- Confusing useState with styling or routing
- Believing useState automatically updates the UI without calling the setter
count with initial value 0 using useState?Solution
Step 1: Recall useState syntax
TheuseStatehook returns an array with two elements: the current state and a function to update it, so destructuring with square brackets is needed.Step 2: Check each option
const [count, setCount] = useState(0); correctly uses array destructuring and callsuseStatewith initial value 0. Others either miss destructuring or call useState incorrectly.Final Answer:
const [count, setCount] = useState(0); -> Option AQuick Check:
useState returns [value, setter] = C [OK]
- Not using array destructuring
- Forgetting to call useState as a function
- Assigning useState to multiple variables without brackets
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
Count: {count}
setCount(count + 1)}>Increment
);
}Solution
Step 1: Understand initial state and button action
The initial count is 0. Each button click callssetCount(count + 1), increasing count by 1.Step 2: Calculate count after two clicks
After first click, count becomes 1. After second click, count becomes 2.Final Answer:
Count: 2 -> Option DQuick Check:
Two increments from 0 = 2 [OK]
- Thinking count resets on each click
- Confusing initial value with updated value
- Ignoring that setCount updates state asynchronously
useState:import React, { useState } from 'react';
function Example() {
const [value, setValue] = useState;
return (
{value}
setValue('Hello')}>Set
);
}Solution
Step 1: Check useState usage
The code assignsuseStatewithout parentheses, so it is not called as a function, which is required.Step 2: Understand consequences
Without callinguseState(),valueandsetValueare undefined, causing errors.Final Answer:
useStateis not called as a function. -> Option CQuick Check:
useState must be called with () = B [OK]
- Forgetting parentheses after useState
- Assuming useState is a variable, not a function
- Misreading error as button or variable issue
useState. Which code correctly implements this behavior?Solution
Step 1: Understand toggle logic with useState
To toggle, state must be boolean and updated to its opposite on click.Step 2: Evaluate each option
const [isOn, setIsOn] = useState(false); setIsOn(!isOn)}>{isOn ? 'ON' : 'OFF'} initializes state as false and toggles it with!isOn, displaying 'ON' or 'OFF' accordingly. Others either don't toggle or misuse state.Final Answer:
const [isOn, setIsOn] = useState(false); setIsOn(!isOn)}>{isOn ? 'ON' : 'OFF'} -> Option AQuick Check:
Toggle state by flipping boolean = A [OK]
- Using string state without toggling
- Setting state to same value repeatedly
- Trying to mutate state variable directly
Ready to prove it?
Pass the test and this pattern moves unknown
