Search

React Event Object

  • Share this:

An event object is an object that represents the event that has occurred. It contains information about the event, such as the type of event, the target element that triggered the event, and any additional data associated with the event.

When an event handler is called in response to an event, the event object is passed as an argument to the event handler function. You can access the properties of the event object to get more information about the event.

Here is an example of a component that handles a click event and accesses the properties of the event object:

import React from 'react';

function Example(props) {
  function handleClick(event) {
    console.log(event.type); // "click"
    console.log(event.target); // the element that was clicked
  }

  return (
    <button onClick={handleClick}>Click me</button>
  );
}

In this example, the component renders a button with an `onClick` event handler that logs the type of the event and the target element that was clicked to the console when the button is clicked.

It's important to note that the event object is a synthetic event created by React and may behave differently than a native browser event. You can find more information about the event object in the React documentation.

Tags:

About author
I am a professional web developer. I love programming and coding, and reading books. I am the founder and CEO of StorialTech.