React Foundations·Components, state and data

State and events

State is a snapshot; updates schedule a new render instead of changing the old one.

Intermediate 13 min read Lesson 7 of 300 Challenge included
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.

Counter.jsxJSX
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

Toggle a bookmark

Easy

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 button
Check 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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 7 of 300 · 0% of course