🎓 A1Learner
🔍
0 of 50 lessons completed

The Event Object

The event object is an object that React passes to an event handler. It carries details about the event that just happened.

The handler receives the event

React passes the event object as the first argument.

function handleClick(e) {
  console.log(e.type);
}

Read the target

e.target is the element that fired the event.

function handleClick(e) {
  console.log(e.target);
}

Stop the default action

preventDefault stops the browser from acting on its own.

function handleSubmit(e) {
  e.preventDefault();
}

Example

function handleClick(e) {
  console.log(e.type);
}

export default function App() {
  return <button onClick={handleClick}>Show type</button>;
}

Key points

  • The handler receives the event object as its first argument.
  • The object is often named e or event.
  • e.type holds the type of the event, such as click.
  • e.target is the element that fired the event.
  • e.preventDefault() stops what the browser would do on its own.

In this example, handleClick takes the event object as e. App returns a button with onClick set to handleClick. A click runs handleClick. handleClick logs e.type to the console. The console shows click. The first render shows a button that reads Show type.

Note

The event object only exists inside the handler that received it.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does React pass to an event handler as its first argument?