React useState Hook
Components often need to change what's on the screen as a result of an interaction. Typing into the form should update the input field, clicking "next" should change the image carousel. Components need to remember things. In React, this is called State.
Adding State to a Component
You can add state by importing theuseStateHook from React. Calling it tells React that you want this component to remember a value across renders.
Anatomy of useState
const [index, setIndex] = useState(0);
It returns an array with two values: 1. The current state (`index`). 2. A setter function (`setIndex`) that lets you update it and trigger a re-render.
State as a Snapshot
State might look like a regular variable, but it behaves more like a snapshot. Setting it does not change the state variable you already have, but instead triggers a re-render where the component receives the new value.
View Full Concept Sheet+
Always treat state as read-only. When your state holds objects or arrays, use the spread operator (`...`) or array methods like `map` and `filter` to create new copies before calling the setter. Never mutate `state.value = something` directly.
