Learn how useState makes your components interactive and reactive!
This message is controlled by state. Try changing your name below!
This counter started at: 0
Started with: "Alex"
Hello, Alex!(4 characters)
Learning React state management! Started with 42 likes and 128 followers.
Current theme: blue (started with: blue)
const [count, setCount] = useState(0);
const [name, setName] = useState("Alex");
const [todos, setTodos] = useState([...]);useState returns [currentValue, setterFunction]. Initial value can be any type.
// Simple update
setCount(count + 1);
// Update from input
setName(e.target.value);
// Update array (create new array)
setTodos([...todos, newTodo]);Always use the setter function. For arrays/objects, create new ones instead of mutating.
{showWelcome && <div>Welcome, {name}!</div>}
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>State variables can be used directly in JSX. Changes trigger re-renders.
initialCount to 100 and reloadstartingLikes and startingFollowersinitialTheme to "purple"initialShowWelcome to falseinitialTodoText to something else