react / react/react

Use Inline Event Handlers for trapBubbledEventsLocal and the iOS Safari Click Hack

Open
#4,166 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: DOM React Core Team Type: Enhancement
Dominant language
JavaScript
Stars
251k
Forks
51.4k
Avg merge
2d 4h
Merged PRs (30d)
53

Description

We currently do a lot of work at the end of mount to find all the nodes and attach listeners after the fact. This is severely impacting initial rendering performance of <form />, <img /> and click handlers.

Instead we can just use inline event handlers in the innerHTML string. For the iOS Safari hack it should be trivial. The handler doesn't even have to do anything.

The inline event handler would need to either redispatch the event, or call into some other event handler system. Probably a global listeners.

window._handleReactEvent = ...;
<img onload="_handleReactEvent(event)">

Since there could potentially be multiple Reacts, they should probably chain the handler if there already is one registered. (Although multiple Reacts in the same document is already pretty broken in this regard.)

It doesn't have to be a global. Since inline event handlers gets the element added as a with(element) scope around itself. It is equivalent to add it to the prototype:

Element.prototype._handleReactEvent = ...;

or

HTMLImgElement.prototype._handleReactEvent = ...;

This makes them a bit more hidden, unobtrusive.

We still need to render this string for server-side rendering to avoid needing to change the HTML or wire up handlers after-the-fact on the client.

Since these events can fire before React has loaded, we need to check for the existence of the handler before it is used.

<img onload="this._handleReactEvent&&_handleReactEvent(event)">

It is critical that this string is short - for innerHTML string concat performance and network performance. Yet it needs to be unlikely to collide with anything else.

Is there a unicode character we could use?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or entry points are named. Start by locating the mount-time listener work for

, , and click handlers, then evaluate the proposed inline-handler path for client rendering and server-side rendering; done requires preserving event behavior without post-mount listener attachment and handling absent or existing handlers.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.