0 day streak
javascript / beginner
Snippet

Handling Click Events

Event handlers allow your application to respond to user interactions. In React, you pass a function to the onClick prop to trigger logic when a button is pressed.

snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
function AlertButton() {
function handleClick() {
alert('Button was clicked!');
}
return (
<button onClick={handleClick}>
Click Me
</button>
);
}
react
Breakdown
1
function handleClick() { ... }
Defines the custom logic that should run when the event occurs.
2
onClick={handleClick}
Attaches the handler function to the button's click event. Note that we pass the function name without parentheses.