State & Events Lesson 7 of 13
React Events — onClick, onChange and onSubmit
Respond to clicks, typing and form submissions in React with onClick, onChange and onSubmit, pass arguments to handlers, and use the event object.
React handles events with camelCase attributes like onClick and onChange. You pass a function, and React calls it when the event happens. Combined with state, events make components interactive.
onClick
<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 VoteButtons() {
const [votes, setVotes] = useState({ yes: 0, no: 0 });
function vote(choice) {
setVotes({ ...votes, [choice]: votes[choice] + 1 });
}
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<h3>Should we have a class outing?</h3>
<button onClick={() => vote("yes")}>👍 Yes ({votes.yes})</button>{" "}
<button onClick={() => vote("no")}>👎 No ({votes.no})</button>
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<VoteButtons />);
</script>
onClick={() => vote("yes")}passes an arrow function that callsvotewith an argument.[choice]: ...uses a computed key, so the same function updatesyesorno.
onChange: reacting to typing
<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 StatusBox() {
const [text, setText] = useState("");
const left = 100 - text.length;
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<textarea
rows="3" cols="36" maxLength={100}
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="What's on your mind?"
/>
<p style={{ color: left < 10 ? "crimson" : "gray" }}>{left} characters left</p>
<p>Preview: {text.toUpperCase()}</p>
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<StatusBox />);
</script>
e is the event object. e.target is the element that fired the event, so e.target.value is the current text. Setting value={text} and updating it in onChange is called a controlled input: React state is the single source of truth.
onSubmit and preventDefault
<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 Login() {
const [user, setUser] = useState("");
const [message, setMessage] = useState("");
function handleSubmit(e) {
e.preventDefault(); // stop the page from reloading
setMessage(user.trim() ? `Welcome, ${user.trim()}!` : "Please enter your name.");
}
return (
<form onSubmit={handleSubmit} style={{ fontFamily: "sans-serif", padding: 16 }}>
<input value={user} onChange={(e) => setUser(e.target.value)} placeholder="Name" />
<button>Log in</button>
<p>{message}</p>
</form>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<Login />);
</script>
Put the handler on the form's onSubmit, not the button's onClick. That way pressing Enter also works.
Other useful events
| Event | Fires when |
|---|---|
onClick | Clicked or tapped |
onChange | An input, select or checkbox changes |
onSubmit | A form is submitted |
onKeyDown | A key is pressed (e.key) |
onFocus / onBlur | An input gains / loses focus |
onMouseEnter / onMouseLeave | The pointer enters / leaves |
Keyboard events
<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 Tags() {
const [input, setInput] = useState("");
const [tags, setTags] = useState(["html", "css"]);
function handleKey(e) {
if (e.key === "Enter" && input.trim()) {
setTags([...tags, input.trim().toLowerCase()]);
setInput("");
}
}
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<input value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={handleKey} placeholder="Type a tag, press Enter" />
<p>{tags.map((t) => <span key={t} style={{ background: "#e3f1ec", padding: "2px 8px", borderRadius: 99, marginRight: 6 }}>#{t}</span>)}</p>
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<Tags />);
</script>
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04