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

Undocumented alteration of onClick arguments #8354

Closed
wikiwong opened this issue Nov 19, 2016 · 10 comments
Closed

Undocumented alteration of onClick arguments #8354

wikiwong opened this issue Nov 19, 2016 · 10 comments

Comments

@wikiwong
Copy link

After upgrading from 15.3.2 to React/ReactDOM 15.4.0 , I noticed that the onClick synthetic event (and possibly others) is receiving arguments differently.

In 15.3.2, the onClick event (without any custom arguments being passed) receives 3 arguments

0 - Proxy
1 - undefined
2 - Event
(observed in the console of this jsfiddle: https://jsfiddle.net/u7b6zhfm/1/)

In 15.4.0, the onClick arguments look like:
0 - Proxy
1 - Event
(Again viewable in the console here: https://jsfiddle.net/ujuzLhse/)

This ended up causing problems in my application because in 15.3.2, I was passing an additional argument to a click handler, and expecting it to be received as the second argument. Once I upgraded to 15.4.0, the argument was being received as the third (after Proxy and Event).

I'm not so sure this is to be considered an "issue" with React, but I didn't see anything in the release notes in the way of event handling, so thought I would raise it just in case.

The way I ended up adjusting for this is by using the spread/rest operator when passing/receiving arguments in the event handler function, which may be the approach I should have taken to begin with

PS: Thank you for all the hard work you guys put into this, super excited for fiber! Rock on 🎸

@aweary
Copy link
Contributor

aweary commented Nov 20, 2016

Thanks for the issue @wikiwong! You can read about what that those additional arguments are/where in this issue: #7902

Basically, an additional argument that ended up being undefined was being bound to the event handler, and that was fixed. You should really just ignore any argument coming after Proxy (which is the SyntheticEvent).

This ended up causing problems in my application because in 15.3.2, I was passing an additional argument to a click handler, and expecting it to be received as the second argument. Once I upgraded to 15.4.0, the argument was being received as the third (after Proxy and Event).

The way I pass additional arguments is to bind them in render when passing in the event handler

<div onClick={this.handleClick.bind(this, id)} />

That way the arguments should come before the SyntheticEvent

handleClick(id, event) {
 // ...
}

Doing it this way you shouldn't be affected by the order or number of arguments that are passed to the event handler, as long as the SyntheticEvent is still passed first by React.

Since this is essentially a bug fix I'm going to close this out, but thanks for taking the time to fill it out!

@aweary aweary closed this as completed Nov 20, 2016
@gaearon
Copy link
Collaborator

gaearon commented Nov 20, 2016

Why is there even a second argument?

@aweary
Copy link
Contributor

aweary commented Nov 20, 2016

@gaearon because we call the bound event handler by dispatching a custom event here using a fake node. So that second argument ends up being the fake event.

@gaearon
Copy link
Collaborator

gaearon commented Nov 20, 2016

Can we make a closure there that omits that argument? Then number of arguments should match in dev and prod.

@aweary
Copy link
Contributor

aweary commented Nov 20, 2016

Can we make a closure there that omits that argument? Then number of arguments should match in dev and prod.

It would mean an extra function call for every event dispatch in dev but yeah, we can do it

@gaearon
Copy link
Collaborator

gaearon commented Nov 20, 2016

Shouldn't be costly as browser events don't come at enormous speeds. Wanna make a PR?

@aweary
Copy link
Contributor

aweary commented Nov 20, 2016

PR opened at #8363

@wikiwong
Copy link
Author

Thanks @gaearon and @aweary !

@wikiwong
Copy link
Author

@aweary fyi I went down the route of binding the event handler in render, but my linter yelled at me. The reasoning I found pretty interesting: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md

leafi added a commit to toggl/react-infinite-calendar that referenced this issue Feb 9, 2017
React.js passes a few extra arguments to event handlers.

Because onDayClick(1) has an additional optional argument, these extra
event handler arguments were being passed along unintentionally.

See facebook/react#8354 for some commentary.

Addresses upstream issue clauderic#60

(1) onDayClick from src/index.js. This gets passed directly into Day's
handleDayClick prop.
@smorin
Copy link

smorin commented Jun 19, 2017

@smorin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants