-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor createEventHandle signature #19174
Conversation
Details of bundled changes.Comparing: 97b96da...eff2510 react-dom
react-art
ReactDOM: size: 0.0%, gzip: -0.0% Size changes (experimental) |
Details of bundled changes.Comparing: 97b96da...eff2510 react-dom
react-art
ReactDOM: size: 0.0%, gzip: -0.0% Size changes (stable) |
fa15c98
to
4125918
Compare
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit eff2510:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a simplification overall which is nice. Questions about the one-listener-per-target+type constraint though.
packages/react-interactions/events/src/dom/create-event-handle/useEvent.js
Show resolved
Hide resolved
packages/react-interactions/events/src/dom/create-event-handle/useEvent.js
Show resolved
Hide resolved
68af22c
to
be0c79a
Compare
Delete target Delete target Add back codes Revise
be0c79a
to
eff2510
Compare
if (targetListeners !== null) { | ||
targetListeners.delete(listener); | ||
} | ||
const registeredReactDOMEvents = new PossiblyWeakSet(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean we leak memory on IE11? It looks like we always add targets to this Set, but it's never cleared (since it's assumed to be a WeakSet in modern browsers).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #19686.
This PR refactors the
ReactDOM.createEventHandle
API so that the signature is simpler and more barebones. Previously,createEventHandle
returned an object with the methodssetListener
andclear
, which mimiced whatuseEvent
does.After utilizing this API internally, primarily behind
useEvent
, it makes little sense forcreateEventHandle
to have this signature. It still makses sense foruseEvent
to have this signtuare, but it's not important that the core API works the same. All we really want this core API to do is add an event and remove and event – sort of like a better nativeaddEventListener
.This change means we no longer return an object from
createEventHandle
, but rather a function that sets the listener. Upon using the setter, you get back an unsubscribe/clear listener function.To show a more concrete example, previously you might use the API in this way:
With the changes in this PR, the above would look like this instead:
This change offers some benefits:
createEventHandle
by not having as much overhead