Session 04: Handling Events
Adding event handlers to components
You can make it show a message when a user clicks by following these three steps:
- Declare a function called handleClick inside your Button component.
- Implement the logic inside that function (use alert to show the message).
- Add
onClick={handleClick}
to the<button>
JSX.
export default function Button() {
const handleClick = () => {
alert('You clicked me!');
};
return <button onClick={handleClick}>Click me</button>;
}