What Is State?
Part of the Fundamentals section of Coddy's React journey — lesson 22 of 42.
Everything so far was frozen: data went in, a page came out, nothing ever changed. Real apps remember things that change: a like count, an open menu, the text in a search box. That memory is called state.
React gives components state through the useState hook:
import { useState } from 'react';
export default function App() {
const [count, setCount] = useState(0);
...
}One line, three pieces:
count: the current valuesetCount: the function that changes ituseState(0): the starting value
The golden rule: never assign to state directly. count = 5 changes a variable React isn't watching. setCount(5) changes the state and re-renders the component. That re-render is what updates the screen.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2JSX Expressions
Curly BracesMath in BracesBraces in AttributesCalling Functions in JSXRecap: JSX Expressions5State & Events
What Is State?Click CounterCount in StepsToggle with StateIndependent StateRecap: State & Events8Project: Profile Card
Step 1: Card StructureStep 2: Card with PropsStep 3: Like ButtonStep 4: Follow Toggle