Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does React create SyntheticEvents?

Tags:

reactjs

Right now, this is the way I understand event handling in React:

  1. The native event capture phase happens
  2. The native event reaches target
  3. The native event bubbles back up
  4. React catches it at the document layer
  5. React puts it in its EventPluginHub
  6. React simulates another full capture/bubble roundtrip for the SyntheticEvent
  7. React runs the handlers you built in your code

If my understanding of React internals is correct, and taking into account this part of the HTML spec:

Event objects are dispatched to an event target. But before dispatch can begin, the event object’s propagation path must first be determined.

Does React wait until the event bubbles up to document to create its SyntheticEvent? And if so, why? In the first step of the event's life, all info about its propagation path is known, so they could do it there.

like image 276
fnune Avatar asked Sep 21 '18 13:09

fnune


People also ask

Does event bubbling happen in React?

Event Capturing in ReactActually both happen one after another: When a user interacts with an element, the DOM API traverses down the document (capturing phase) to the target element (target phase), only then the DOM API traverses up again (bubbling phase).

Why do synthetic events React?

Benefits of using synthetic events:Cross browsers applications are easy to implement. Synthetic events are that ReactJS reuses these events objects, by pooling them, which increase the performance.

How Synthetic events work in React?

In order to work as a cross-browser application, React has created a wrapper same as the native browser in order to avoid creating multiple implementations for multiple methods for multiple browsers, creating common names for all events across browsers.

How are forms created in React?

In React, form data is usually handled by the components. When the data is handled by the components, all the data is stored in the component state. You can control changes by adding event handlers in the onChange attribute and that event handler will be used to update the state of the variable.


1 Answers

One of the reasons is performance. Rather than attaching event listeners to every event you use inside the app, React attaches a single event listener for every event type to document. Then, when you create a, for example, onClick event on a div, it just adds that to it's internal event listener map. Once click on a div happens, React's listener on document node finds your event listener and calls it with SyntheticEvent.

This way, react doesn't have to re-create and clear listeners from DOM all the time, it just changes it's internal listener registry.

The other reason is that React's event bubbling works differently compared to DOM one in some cases. Portals in particular.

Take this for example:

const MyComponent = () => (
  <div onClick={() => console.log('I was clicked!')}>
    MyComponent
    <SomeModalThatUsesPortalComponent>A modal</SomeModalThatUsesPortalComponent>
  </div>
);

const SomeModalThatUsesPortalComponent = () => {
  return ReactDOM.createPortal(
    <div onClick={() => console.log('Modal clicked!')}>this.props.children</div>,
    document.getElementById('myModalsPortal')
  );
}

This would end up with following DOM:

<body>
  <div>
    My Component
  </div>
  <div id="myModalsPortal">
    <div>A modal</div>
  </div>
</body>

So in this case when using a Portal, DOM structure doesn't match the component structure exactly. Here is where bubbling behaviour diverges:

  • Clicking on A modal will not trigger a native click event on MyComponent's div
  • It will trigger React's onClick handler on MyComponent's div, because modal is a child component of MyComponent
like image 180
Igor Pantović Avatar answered Oct 26 '22 09:10

Igor Pantović