Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
useState is a React Hook that lets you add state to function components. State is data that can change over time and trigger re-renders.
setState, React re-renders your component with the new value!First, import useState from React at the top of your component file.
Pro Tip
You can import multiple hooks: { useState, useEffect, useContext }.
Call useState with an initial value to create a state variable.
Key Point
useState returns an array: [currentValue, setterFunction]. Use array destructuring!
Display the state value in your component's JSX.
Important
Use curly braces {} to embed JavaScript values in JSX. State values are reactive!
Use the setter function to update the state value.
Key Point
State updates trigger re-renders! Use functional updates when new state depends on previous state.
Click buttons to see state updates
function Counter() {
// Declare state variable with initial value 0
const [count, setCount] = React.useState(0);
return (
<div className="container">
<h1>🔢 Counter</h1>
<div className="counter-display">
{count}
</div>
<div className="buttons">
<button
onClick={() => setCount(count - 1)}
className="btn-decrement"
>
- Decrement
</button>
<button
onClick={() => setCount(0)}
className="btn-reset"
>
Reset
</button>
<button
onClick={() => setCount(count + 1)}
className="btn-increment"
>
+ Increment
</button>
</div>
<div className="info-box">
<h3>How it works:</h3>
<ul>
<li>• useState(0) creates state with initial value 0</li>
<li>• count holds the current value</li>
<li>• setCount updates the value</li>
<li>• Component re-renders on each update</li>
</ul>
</div>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Counter />);Loading preview...
When updating state based on the previous state, always use the functional form. This ensures you're working with the latest state value!
If multiple updates happen quickly, some may be lost!
React guarantees you get the latest value!
Each input has its own state
function UserForm() {
// Multiple useState calls for different data
const [name, setName] = React.useState('');
const [email, setEmail] = React.useState('');
const [age, setAge] = React.useState('');
const [submitted, setSubmitted] = React.useState(false);
function handleSubmit(e) {
e.preventDefault();
setSubmitted(true);
setTimeout(() => setSubmitted(false), 3000);
}
return (
<div className="container">
<h1>📝 User Form</h1>
{submitted && (
<div className="success">
✅ Form submitted successfully!
</div>
)}
<form onSubmit={handleSubmit}>
<div className="field">
<label>Name</label>
<input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="John Doe"
required
/>
</div>
<div className="field">
<label>Email</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="john@example.com"
required
/>
</div>
<div className="field">
<label>Age</label>
<input
type="number"
value={age}
onChange={(e) => setAge(e.target.value)}
placeholder="25"
required
/>
</div>
<button type="submit" className="btn-submit">
Submit
</button>
</form>
{name && (
<div className="preview">
<h3>Preview:</h3>
<p><strong>Name:</strong> {name}</p>
<p><strong>Email:</strong> {email}</p>
<p><strong>Age:</strong> {age}</p>
</div>
)}
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<UserForm />);Loading preview...
useState returns [currentValue, setterFunction]. Use array destructuring to name them.
Calling setState causes component to re-render with the new state value.
Use updater function when new state depends on previous: setState(prev => prev + 1)
You can call useState multiple times in one component for different state variables.