DEV Community

Aman Kureshi
Aman Kureshi

Posted on

πŸ–±οΈ Event Handling in React β€” Making Your UI Interactive

Event handling in React is how you respond to user actions like clicks, typing, submitting forms, etc. It works similarly to plain JavaScript but with a React-friendly syntax.

πŸ‘‹ Example:
function Button() {
const handleClick = () => {
alert("Button clicked!");
};
return <button onClick={handleClick}>Click Me</button>;
}

πŸ“Œ Key points:

β€’ Events are written in camelCase: onClick, onChange, onSubmit
β€’ You pass a function, not a string
β€’ React wraps events in a SyntheticEvent for cross-browser consistency

πŸ’‘ You can also pass arguments:
<button onClick={() => handleClick("Aman")}>Greet</button>

Event handling is the core of creating dynamic and interactive UIs in React. Mastering it gives you full control over user interactions.

Top comments (0)