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

Input with type=radio incorrectly trigger onChange handler #10078

Closed
bacher opened this issue Jun 30, 2017 · 11 comments
Closed

Input with type=radio incorrectly trigger onChange handler #10078

bacher opened this issue Jun 30, 2017 · 11 comments

Comments

@bacher
Copy link

bacher commented Jun 30, 2017

Bug.

If I put several input type="radio" with same name (as in jsfiddle example below) onChange handler triggered only once for individual input when mouse click on input happen.

<input type="radio" name="some" onChange={...} />
<input type="radio" name="some" onChange={...} />
...

Example: https://jsfiddle.net/Bacher/jxrar3ke/7/
Try to click on different radio inputs several times. Next clicks has no console.log output.

I expect trigger onChange every time when I click on non-selected radio input.

Bug reproduces in several latest versions of React, at least 15.6.0 and 15.6.1.
My OS is Ubuntu. I try in latest Chrome and latest Firefox.

@selbekk
Copy link
Contributor

selbekk commented Jun 30, 2017

I don't see this as a bug, but as the expected behavior - if you need that behavior, write an onChange handler that triggers the behavior correct for your scenario based on which one was triggered.

@bacher
Copy link
Author

bacher commented Jun 30, 2017

@selbekk I think you misunderstood me.

Try this scenario in my fiddle example:

  • click on "1";
  • click on "2";
  • click on "1".

When you click "1" second time, onChange will not be triggered.

@selbekk
Copy link
Contributor

selbekk commented Jun 30, 2017

Ah sorry, I did indeed misunderstand your question.

This is my theory:

When you click a radio button for the first time, you trigger its change event. However, when you unselect it (by clicking another radio button), you don't trigger one. This is because suddenly, the DOM keeps some state for you - but that's React's job (unless you want to use "uncontrolled components", which is discouraged in most cases). And since React does some nice trickery with events (they're synthesized to improve performance), this might be one of the issues that arises.

If you set the checked attribute and keep which choice is set in state, you'll have more luck :)

Here's a fork of your pen that does that: https://jsfiddle.net/npefL71p/

I agree it's a bit weird behavior

@bacher
Copy link
Author

bacher commented Jun 30, 2017

Yes, I already fix my trouble by extract state, right as you write above.

I report this bug because I think using uncontrolled inputs is not a bad way. Sometimes it simpler :)

I think developers forgot case when input[type=radio] used as group select (in case with same name), however radio group is common way for past generation of web :)

@allanesquina
Copy link

Hey guys,I had a similar probelm here because I was using defaultChecked prop instead checked, after change to checked it's working again.

Thanks @bacher @selbekk 👍

@rhinoceraptor
Copy link

I am having this same bug in my app (it took me quite a while to narrow it down!).

This is a regression - this functionality seems to have been broken in version 15.6.0.

I have prepared two CodePens demonstrating the issue, one with version 15.6.0 (broken) and one with 15.5.4 (working).

To reproduce, click 1, then click 2, and then click 1 again.

@gaearon
Copy link
Collaborator

gaearon commented Jul 11, 2017

cc @jquense

@jquense
Copy link
Contributor

jquense commented Jul 11, 2017

Duplicate of #9988 I believe

@gaearon
Copy link
Collaborator

gaearon commented Jul 11, 2017

Thanks! Do you think you could look into this?

@jquense
Copy link
Contributor

jquense commented Jul 11, 2017

The problem is that DOMInputComponent doesn't update the value tracking bits in updateCousins for radios/checkboxes. It's a easy fix just haven't had a moment to PR something

@gaearon gaearon changed the title Bug: Input with type=radio incorrectly trigger onChange handler Input with type=radio incorrectly trigger onChange handler Oct 4, 2017
@gaearon
Copy link
Collaborator

gaearon commented Oct 4, 2017

Seems like it was fixed with #9988.

@gaearon gaearon closed this as completed Oct 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants