# State & useState

> Give your components a memory that persists across renders and triggers UI updates.

Subject: React | Lesson 8 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/state-basics
Previous: [Props & Destructuring](https://codewithanimation.in/react/props-and-destructuring.md) | Next: [Functional State Updates](https://codewithanimation.in/react/functional-state-updates.md)

## What it is

**State** is a component's memory. It allows a component to remember data between renders.

In functional components, you add state using the `useState` hook. It returns an array with exactly two values: the current state value, and a setter function to update it.

## Why regular variables don't work

If you try to use a standard local variable (`let count = 0`) and update it (`count++`) on a button click, two things go wrong:

1. React doesn't know the variable changed, so it won't re-render the screen to show the new value.

2. Even if React did re-render, the function would run from the top again, resetting `let count = 0` back to zero.

`useState` solves both problems: it persists the data across renders, and calling the setter function tells React to update the screen.

## How to use it

You call `const [count, setCount] = useState(0);`. The `0` is the initial value.

When the user clicks a button, you call `setCount(count + 1)`. This does not change the `count` variable immediately. Instead, it queues a re-render. React then calls your component function again, this time providing `1` as the new `count`.

## Common mistake: The direct mutation trap

Never mutate state directly: `count = 1` or `user.name = 'Alice'`. React relies on you calling the setter function (`setCount` or `setUser`) to know that an update occurred.

If you mutate the variable directly, React is completely unaware, and your UI will be out of sync with your data.

## Tip: State is a snapshot

Think of state as a photograph taken at the moment of render. When you call `setCount(count + 1)`, it doesn't change the `count` variable in the current render. It requests a *new* render with the *new* value.

## Try it: The Memory Test

Compare a standard local variable against a React state variable. Notice how only the state variable manages to update the screen and remember its value.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/state-basics

## Check yourself

### Question 1

Why can't you just use standard local variables (e.g., `let x = 0`) for component data that changes?

- A. Because `let` is not supported in JSX.
- B. Because standard variables don't trigger re-renders, and their values are reset every time the component function runs.
- C. Because standard variables are not garbage collected.

Correct answer: B

Without `useState`, React doesn't know it needs to update the screen, and local variables are recreated from scratch on every function call.

- Why not A: JSX supports standard JavaScript.
- Why not C: They are garbage collected just fine.

### Question 2

What does the `useState` hook return?

- A. An object with `value` and `update` properties.
- B. An array containing exactly two items: the current state value, and a function to update it.
- C. A proxy object that automatically triggers renders when mutated.

Correct answer: B

It returns an array `[value, setter]` so you can easily destructure and name them whatever you want: `const [age, setAge] = useState(25)`.

- Why not A: It returns an array, not an object.
- Why not C: That describes Vue or MobX, not React.

### Question 3

What happens if you directly modify a state variable (e.g., `count = 5`) instead of using the setter function?

- A. React throws a compilation error.
- B. React updates the Virtual DOM but not the real DOM.
- C. The variable changes in memory, but React does not re-render the screen to show the new value.

Correct answer: C

React is completely unaware of direct mutations. Calling the setter function is the only way to notify React that it needs to redraw the UI.

- Why not A: React won't stop you; it's valid JavaScript. But it's a bug.
- Why not B: React doesn't update anything because it doesn't know the state changed.

## Remember this

- State allows a component to remember data between renders.
- Calling the setter function triggers a re-render.
- Never mutate state directly; always use the setter function.
- State variables are snapshots of the current render, not live reactive proxies.
