Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

state-usestate

State with useState

Last reviewed Jun 1, 2026 Content v20260601
Track mode
client_react
Means
In-browser React TSX
Reading
~2 min
Level
beginner

This lesson

This lesson teaches State with useState: the concepts, APIs, and habits you need before advancing in React.

Local component state is the backbone of interactive UI; mistakes here cause subtle re-render bugs.

You will apply State with useState in contexts like: SPAs, dashboards, design-system-driven products, and React Native mobile apps.

Write TypeScript/TSX, click Run in browser—React 18 loads from CDN, JSX compiles in the tab, UI renders in the preview root, and printOutput feeds the terminal.

When you can explain the previous lesson's ideas without copying starter code.

State is data that changes over time inside a component. useState returns the current value and a setter function. When state updates, React re-renders the component.

Basic pattern

const { useState } = React;
const [count, setCount] = useState(0);

Rules of hooks

  • Only call hooks at the top level of a component—not inside loops, conditions, or nested functions.
  • Only call hooks from React function components or custom hooks.
  • State updates are asynchronous; React may batch multiple setters in one render.

Functional updates

When the next state depends on the previous value, use the updater form: setCount((c) => c + 1). This avoids stale closures in rapid clicks or effects.

Important interview questions and answers

  1. Q: Props vs state?
    A: Props come from parent and are read-only; state is owned and updated inside the component.
  2. Q: Why functional setState?
    A: Guarantees you compute from the latest state when multiple updates happen close together.

Self-check

  1. What triggers a re-render after setCount?
  2. Why can’t you call useState inside an if?

Challenge

Click counter

  1. Click the button three times.
  2. Change the increment to add 2 per click using a functional updater.

Done when: the count increases by 2 on each click.

Challenge

Counter from scratch

  1. Add a second button that subtracts.
  2. Prevent count from going below zero.

Done when: counter cannot display negative values.

Interview prep

How does useState behave?

Updates batch asynchronously; use the updater callback when the next value depends on the previous. Never mutate objects/arrays in place.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Stale state you avoided?
  • Updater function when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump