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

Bug: React 17.0.0-rc.0 doesn't trigger onFocus/onBlur on element.dispatchEvent #19606

Closed
diegohaz opened this issue Aug 13, 2020 · 1 comment
Closed
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@diegohaz
Copy link

React version: 17.0.0-rc.0

Steps To Reproduce

  1. Programmatically dispatch focus/blur events using element.dispatchEvent
  2. Check if onFocus/onBlur props are triggered

Link to code example: https://codesandbox.io/s/react-1700-rc0-dispatching-focusblur-events-pt6ih

The current behavior

onFocus/onBlur props aren't triggered.

The expected behavior

onFocus/onBlur props to be triggered just like in React 16.13.1

@diegohaz diegohaz added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Aug 13, 2020
@gaearon
Copy link
Collaborator

gaearon commented Aug 13, 2020

This is expected: https://reactjs.org/blog/2020/08/10/react-v17-rc.html#aligning-with-browsers

React onFocus and onBlur events have switched to using the native focusin and focusout events under the hood, which more closely match React’s existing behavior and sometimes provide extra information.

This works:

    ref.current.dispatchEvent(
      new Event("focusin", {
        bubbles: true
      })
    );
    ref.current.dispatchEvent(
      new Event("focusout", {
        bubbles: true
      })
    );

https://codesandbox.io/s/react-1700-rc0-dispatching-focusblur-events-forked-kbbul

Note though that you probably want to call .focus() instead of emulating events because the browser fires them in a specific order and it's always a risk to try to replicate it yourself because it might not match what the browser does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants