Event Handling (React)

Event handling in React is responding to user actions like clicks, typing, or form submissions. You attach a function to an element using camelCase props such as onClick and onChange.

React

React events look like HTML events but use camelCase names (onClick, onChange, onSubmit) and take a function rather than a string. You usually pass the function itself, not a call to it: onClick={handleClick}, not onClick={handleClick()}.

Each handler receives a SyntheticEvent, React's cross-browser wrapper around the native event. From it you can read things like e.target.value (the input's text) and call e.preventDefault() to stop default browser behavior, such as a form reloading the page.

Examples

JSX — Clicking the button runs handleClick, which shows an alert dialog reading 'Button clicked!'.

function ClickLogger() {
  function handleClick() {
    alert("Button clicked!");
  }
  return <button onClick={handleClick}>Click me</button>;
}

Learn it in a lesson

See also

Back to the Dictionary