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)