Preparing your workspace
React lets components respond to browser events using event handler props such as onClick, onChange, and onSubmit.
React uses familiar browser event concepts with a consistent component-based API. Event handlers are functions that run when users interact with the page. For buttons, onClick is common. Inputs often use onChange to keep state synchronized with user input. Forms commonly use onSubmit. Event handlers can access information from the event object, and they can call state setters or other application functions. Good event handling keeps UI behavior predictable and avoids unnecessary inline logic when handlers become complex.
A quiz component can use onClick for answer choices and onSubmit for submitting the final answer.
import { useState } from 'react';
export default function SearchBox() {
const [query, setQuery] = useState('');
return (
<input
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder="Search courses"
/>
);
}The input value stays synchronized with the query state as the user types.