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)