Register to get access to free programming courses with interactive exercises

Events JS: React

At first glance, it might seem like React uses normal browser events, but that's not true. React independently intercepts all events that occur in the DOM and broadcasts them to the back-end system.

Any event handler receives a SyntheticEvent type object, a cross-browser wrapper over the native event object. Interface-wise, it's no different from the native wrapper, except that it works the same way in all browsers.

class Component extends React.Component {
  onClick = (event) => {
    console.log(event); // => SyntheticBaseEvent
    console.log(event.type); // => "click"
  }

  // ...
}

SyntheticEvent stores the original event object and provides an interface to access its properties and methods. This interface is the same for all browsers, which is extremely convenient in terms of development. For example, you need to override the default action (page reloading) when submitting a form:

See the Pen js_react_events_prevent_default by Hexlet (@hexlet) on CodePen.

As you can see, nothing out of the ordinary. At the same time, in plain HTML, you can get this sort of behavior in another way. It's enough to just return false from the handler. This won't work in React.

The same should be done when you need to prevent an event from popping up. Only instead of preventDefault the stopPropagation function is called.

The course JS: DOM API said that it's preferable to use addEventListener when working with HTML. One of the main reasons is that it allows you to attach a lot of handlers, which is what many JS add-ons take advantage of. In React, you just don't need this way of working because event flow control is always explicit. No one can connect to React from the outside and attach their handlers to it.

The second thing that can scare developers is attaching handlers directly in JSX. It's worth remembering that JSX is JS code, not HTML. Therefore, it's not a problem. As you'll see later, this code is very easy to read because everything is in one place.

React normalizes events so that they have consistent properties across browsers. In addition, forms can also have the onChange, event, which does exactly what it says on the tin, and greatly simplifies work.


Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.