State and events
State is a snapshot; updates schedule a new render instead of changing the old one.
What you will be able to do
- Use useState for values that change over time and affect rendering.
- Never mutate objects or arrays in state; create new ones.
- Lift state to the closest common parent when two siblings need it.
In plain English
Calling a state setter does not mutate the current value. It schedules a render with a new value. Use functional updates when the next value depends on the previous one, and keep derived values out of state.
import { useState } from 'react'
export default function Counter() {
const [count, setCount] = useState(0)
return (
<button onClick={() => setCount((current) => current + 1)}>
Clicked {count} times
</button>
)
}Your turn
EasyToggle a bookmark
Add a saved boolean and a button that toggles it. Show "Saved" or "Save" based on state.
JSX · starter
const [saved, setSaved] = useState(false)
// add a buttonCheck your understanding before moving on.
Knowledge checkQuestion 1
When should you use a functional state update?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.