Responding to events

Intermediate

Introduction

So far the components only show things. Interfaces also respond: a button is clicked, a key is pressed, a form is submitted. In React you respond by giving an element an event handler, a function React calls when the event happens. This lesson covers the handlers you will use most, and the patterns for passing them around.

How it works

Event props are named in camelCase: onClick, onChange, onKeyDown, onSubmit, onFocus. Their value is a function:

function handleClick() {
  console.log("Clicked!");
}
<button onClick={handleClick}>Click me</button>

Pass the function itself. Writing onClick={handleClick()} calls it immediately while rendering and passes its result, usually undefined, which is a common bug.

To pass arguments, wrap the call in an arrow function: onClick={() => pick("Tea")}. The arrow function is what React calls; it then calls pick with the argument.

React passes every handler an event object. event.target is the element the event happened on; event.key is the key pressed in onKeyDown; for a form, event.target.elements.q is its field named q. Some events have a default browser action: submitting a form reloads the page. Call event.preventDefault() in onSubmit to handle the form yourself.

Components can accept handlers as props, which is how a child tells its parent that something happened. By convention such props are named onSomething, like onPick, and the parent's functions handleSomething, like handlePick. The child decides when to call it; the parent decides what happens. A DrinkButton does not need to know what picking a drink does.

Handlers often need to change what the page shows. Changing a variable does not make React render again; for that you need state, the subject of the next lesson. Here the handlers print to the console, which you can see under the preview.

Example

App.jsx
console.log output from the page shows here.

Line by line

  1. DrinkButton gets the drink and an onPick handler as props, and calls onPick(drink) when clicked.
  2. App decides what picking means: it prints a message.
  3. handleSubmit stops the page from reloading, then reads the field q from the form.
  4. The input uses defaultValue, which sets its starting text and then lets the user type.

Expected result

The preview shows Tea and Coffee buttons and a search form holding "react". Clicking Coffee prints "You picked Coffee" in the console under the preview; pressing Search prints "Searched for react", and the page does not reload.

In the real world

Every interactive element of a React app is wired this way: menus, modals, drag and drop, keyboard shortcuts. Component libraries expose handler props such as onChange, onClose and onSelect, and the parent decides what each does. Analytics code often wraps handlers to record which buttons people click.

Common mistakes

  • Calling the handler while rendering: onClick={save()} instead of onClick={save}.
  • Forgetting preventDefault on a form, which reloads the page and loses what was typed.
  • Writing onclick in lower case, which React does not recognise.
  • Putting handlers on elements that are not interactive, such as a div, instead of a button, which keyboard users cannot reach.

Best practices

  • Name handler props onSomething and handler functions handleSomething.
  • Use real buttons and forms, so keyboard and screen-reader users can use them too.
  • Keep handlers short; move logic into named functions.

Summary

Event handlers are functions passed to props such as onClick, onKeyDown and onSubmit. Pass the function, not the result of calling it, and use an arrow function to pass arguments. The event object describes what happened, preventDefault stops the browser's own action, and handlers passed down as props let children tell their parents about events.

Quick check

1. Which is correct?
2. How do you pass the argument 'Tea' to pick on click?
3. Why call event.preventDefault() in onSubmit?
4. What should a child component's click prop be called, by convention?

Key takeaways

  • Pass the function itself: onClick={handleClick}, not onClick={handleClick()}.
  • Use an arrow function to pass arguments: onClick={() => pick(drink)}.
  • Call event.preventDefault() in onSubmit, and pass handlers down as props named onSomething.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.