Skip to main content

Session 04: Handling Events

https://react.dev/learn/responding-to-events

Adding event handlers to components

You can make it show a message when a user clicks by following these three steps:

  1. Declare a function called handleClick inside your Button component.
  2. Implement the logic inside that function (use alert to show the message).
  3. Add onClick={handleClick} to the <button> JSX.
export default function Button() {
const handleClick = () => {
alert('You clicked me!');
};

return <button onClick={handleClick}>Click me</button>;
}