State & Events Lesson 6 of 13
React State with useState
Remember changing data with the useState hook — counters, toggles and text — understand re-rendering, update state correctly with the updater function, and store objects and arrays.
State is data that belongs to a component and can change over time: a counter, whether a menu is open, what the user typed. When state changes, React re-renders the component so the page shows the new value.
useState
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
const { useState } = React;
function Counter() {
const [count, setCount] = useState(0);
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<h2>Attendance: {count}</h2>
<button onClick={() => setCount(count + 1)}>+ Present</button>{" "}
<button onClick={() => setCount(count - 1)} disabled={count === 0}>− Undo</button>{" "}
<button onClick={() => setCount(0)}>Reset</button>
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<Counter />);
</script>
useState(0) returns two things, unpacked with array destructuring:
count: the current value (starts at 0).setCount: a function to change it. Calling it tells React "this changed, please re-render".
Functions starting with use are called hooks. They must be called at the top level of a component, never inside if statements or loops.
Why not a normal variable?
function Broken() {
let count = 0;
return <button onClick={() => { count++; }}>Count: {count}</button>;
}
This never updates. Changing a normal variable doesn't tell React to re-render, and even if it did, count would reset to 0 every time the function runs. State survives between renders; normal variables don't.
Toggling a boolean
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
const { useState } = React;
function Flashcard() {
const [showAnswer, setShowAnswer] = useState(false);
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<h3>What is the capital of the Philippines?</h3>
{showAnswer && <p style={{ color: "#0f5c4d", fontWeight: "bold" }}>Manila 🇵🇭</p>}
<button onClick={() => setShowAnswer(!showAnswer)}>
{showAnswer ? "Hide answer" : "Show answer"}
</button>
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<Flashcard />);
</script>
Several pieces of state
A component can call useState as many times as it needs:
const [name, setName] = useState("");
const [grade, setGrade] = useState(7);
const [isOfficer, setIsOfficer] = useState(false);
The updater function
When the new value depends on the old one, pass a function to the setter. React gives it the latest value:
setCount(c => c + 1);
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
const { useState } = React;
function App() {
const [a, setA] = useState(0);
const [b, setB] = useState(0);
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<p>Plain: {a} <button onClick={() => { setA(a + 1); setA(a + 1); setA(a + 1); }}>+3?</button></p>
<p>Updater: {b} <button onClick={() => { setB(x => x + 1); setB(x => x + 1); setB(x => x + 1); }}>+3</button></p>
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
The first button only adds 1: all three calls used the same old value of a. The updater form adds 3 correctly.
Objects and arrays in state
Never change state objects or arrays directly. Create a new one with the change, usually with the spread operator ...:
const [student, setStudent] = useState({ name: "Ana", grade: 9 });
setStudent({ ...student, grade: 10 }); // copy, then change grade
const [items, setItems] = useState(["Bigas"]);
setItems([...items, "Itlog"]); // add
setItems(items.filter(i => i !== "Bigas")); // remove
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04