State (React)
State is data that a React component owns and can change over time, such as the current value of an input or whether a menu is open. When state changes, React automatically re-renders the component to reflect the new value.
React
Unlike props, which come from the parent and are read-only, state is internal to a component and can be updated in response to events like clicks or typing. You declare state in a functional component with the useState Hook.
The key idea is that you never update the screen directly — you update the state, and React re-renders the UI to match. This declarative model is what makes React apps predictable.
Examples
JSX — Renders a button whose label flips between 'OFF' and 'ON' each time it is clicked.
import { useState } from "react";
function Toggle() {
const [on, setOn] = useState(false);
return (
<button onClick={() => setOn(!on)}>
{on ? "ON" : "OFF"}
</button>
);
}