Loading Depth Study
Preparing your workspace
State is data managed by a component that can change over time and cause the component to render again.
Props come from a parent, while state is managed by the component itself. The useState Hook provides a state value and a setter function. When the setter is called with a new value, React schedules a re-render. State is useful for counters, form values, selected tabs, open menus, filters, and many other changing UI conditions. State updates should be treated as creating new state rather than directly mutating existing state.
A course lesson page can store whether the lesson is bookmarked, whether a quiz is open, and which answer is selected using state.
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
</div>
);
}The displayed count increases when the button is clicked.