Skip to content
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

ReactDOM.useEvent: support custom types #18351

Merged
merged 2 commits into from
Mar 20, 2020

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Mar 19, 2020

This PR adds support for custom user events to the useEvent system. I've also added tests demonstrating them working as expected. I also added an unnecessary phasedRegistrationNames == null check in accumulateTwoPhaseListeners that never occurs at runtime. This essentially allows this:

function Component() {
  const ref = useRef(null)
  const foo = ReactDOM.unstable_useEvent('foo')

  useEvent(() => {
    foo.setListener(ref.current, () => {
      console.log('Custom user events work!');
    });
  });

  return <span ref={ref}>;
}

const event = document.createEvent('Event');
event.initEvent('foo', true, true);
document. getElementsByName('span')[0].dispatchEvent(event);

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 19, 2020

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 84afbf1:

Sandbox Source
vigorous-taussig-0mnl6 Configuration

@sizebot
Copy link

sizebot commented Mar 19, 2020

No significant bundle size changes to report.

Size changes (stable)

Generated by 🚫 dangerJS against 84afbf1

@sizebot
Copy link

sizebot commented Mar 19, 2020

No significant bundle size changes to report.

Size changes (experimental)

Generated by 🚫 dangerJS against 84afbf1

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving in advance, with my usual nits

custom = ReactDOM.unstable_useEvent('custom-event');
}).toWarnDev(
'Warning: The event "type" provided to useEvent() does not have a known priority type. ' +
'It is recommended to provide a "priority" option to specify a priority.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is old, but it's only obvious to me now that this isn't clear. What are the possible values of priority? Where can I learn more about it? etc. In a future PR this message should be made verbose, especially if we expect people to start using custom events.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The possible values are 0, 1, 2 – for Discrete, user blocking and continious. I think we'll need to expose them from an enum on React maybe, but right now I'm unsure how that would work.

@trueadm trueadm force-pushed the support-custom-types branch 2 times, most recently from 3f19be9 to bce8460 Compare March 20, 2020 14:34
Address feedback

Address feedback

Address feedback
@trueadm trueadm merged commit dbd85a0 into facebook:master Mar 20, 2020
@trueadm trueadm deleted the support-custom-types branch March 20, 2020 14:45
trueadm added a commit to trueadm/react that referenced this pull request Mar 20, 2020
* ReactDOM.useEvent: support custom types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants