React Events are the ways we capture user actions on the screen. Whenever a user interacts with the UI, an event is triggered.
We use "Event Handlers" (functions) to tell React what to do when these actions happen.
Example: You click a button → handleClick() runs → an Alert pops up or the text changes.
In HTML it's onclick, but in React we use camelCase: onClick.
Pass the function name, don't call it immediately.
// ✅ Correct
<button onClick={handleClick}>
// ❌ Wrong
<button onClick="handleClick()">
Use { } to wrap the function name, not quotes.
function App() {
function handleClick() {
alert("Button Clicked!");
}
return (
<div>
<h1>React Events</h1>
<button onClick={handleClick}>
Click Me
</button>
</div>
);
}
export default App;
Events are most powerful when they update State.
import { useState } from "react";
function App() {
const [message, setMessage] = useState("Hello");
return (
<div>
<h1>{message}</h1>
<button onClick={() => setMessage("Welcome to React")}>
Change Text
</button>
</div>
);
}
| Event | Description | Target Element |
|---|---|---|
onClick | Triggered on a mouse click | Button, Div, A |
onChange | Triggered when value changes | Input, Select, Textarea |
onSubmit | Triggered when form is sent | Form |
onKeyDown | Triggered on key press | Input, Body |
| Feature | HTML Events | React Events |
|---|---|---|
| Naming | lowercase (onclick) | camelCase (onClick) |
| Handler | String ("click()") | Function ({click}) |
| Performance | Native DOM | Synthetic Events (Faster) |
React uses Synthetic Events to ensure your code works identically across all browsers.
Challenge: Create a "Secret Message" button that toggles visibility (Show/Hide).
Props → State → Events (Complete)
↓
Next Up: React Hooks (useEffect, etc.) → API Integration