You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
onChange doesn't fire if a capture-phase document listener for the underlying native event calls setState() #12643
Do you want to request a feature or report a bug?
A bug
What is the current behavior?
Checkbox not fires onChange for controlled component, it somehow related to global event listeners with setState, see sandbox example.
What is the expected behavior?
Checkbox should fire onChange handler
Even if that was supported in REACT 15, I am not sure that it's a good idea to add that flexibility in REACT 16.
It would maybe be more beneficial to add that in the migration notes.
At least that would enforce people to write better HTML 😉
Reacted by Artur Charaev, Oleg Gun, Vanya Dymovsky, Vahid Boreiri, Rishichandra Wawhal, Billy Janitsch, RedFing, shane, Kingsley Chen, Steven Koch and 2 more
@stmoreau thank you, but input dont have to be nested in label and my example is totally valid, you might read it at usage notes section on MDN by link you left. I also suspect that it fixes checkbox only due to nesting, because second checkbox in my example was nested in a div and worked fine. So guess you messing things up.
@ad1992 yes, thank you, there is workarounds, the sad part - sometimes you cant control it, like if we have 3rd party UI lib. Assume its wide issue because global onclick event listener with capture phase used almost everywhere to capture clicks outside elements (popovers, datepickers, selects and etc).
changed the title [-]16 version breaks checkbox onChange[/-][+]Checkbox doesn't fire onChange in the presence of a capture-phase document click listener[/+]on Aug 16, 2018
changed the title [-]Checkbox doesn't fire onChange in the presence of a capture-phase document click listener[/-][+]Checkbox doesn't fire onChange in the presence of a capture-phase document click listener that calls setState()[/+]on Aug 16, 2018
While #10830 would fix it, I don't think it really solves the underlying problem which has to do with the input tracking mechanism.
Here's a repro case that only uses regular inputs, not checkboxes:
classAppextendsReact.Component{state={value: ''}handleChange=(e)=>{this.setState({value: e.target.value});}componentDidMount(){document.addEventListener("input",()=>{// COMMENT OUT THIS LINE TO FIX:this.setState({});},true);}render(){return(<div><inputvalue={this.state.value}onChange={this.handleChange}/></div>);}}ReactDOM.render(<App/>,document.getElementById("container"));
Typing doesn't work — unless I comment out that setState call in the capture phase listener.
Say the input is empty and we're typing a.
What happens here is that setState({}) in the capture phase non-React listener runs first. When re-rendering due to that first empty setState({}), input props still contain the old value ("") while the DOM node's value is new ("a"). They're not equal, so we'll set the DOM node value to "" (according to the props) and remember "" as the current value.
Then, ChangeEventPlugin tries to decide whether to emit a change event. It asks the tracker whether the value has changed. The tracker compares the presumably "new" node.value (it's "" — we've just set it earlier!) with the lastValue it has stored (also "" — and also just updated). No changes!
Our "a" update is lost. We never get the change event, and never actually get a chance to set the correct state.
changed the title [-]Checkbox doesn't fire onChange in the presence of a capture-phase document click listener that calls setState()[/-][+]onChange doesn't fire if a capture-phase document listener for the underlying native event calls setState()[/+]on Aug 17, 2018
There does seem to be a difference in 16 and 15 behavior though. I'm not sure if it's limited to checkboxes or other inputs too. Maybe it's between setState being in same or different components.
In particular I don't understand why in this example we end up committing input update at all, or even reconciling it (we get into the complete phase). I'd expect it to bail out because setState was in the Counter. Or maybe our bail out doesn't prevent complete phase from running? That would explain why wrapping in a div "fixes" it. Seems like that's a more generic problem that we can fix.
Activity
stmoreau commentedon Apr 18, 2018
Actually, the HTML elements are structured in a wrong way!
Based on:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label
your elements should be like so:
<label>Click me <input type="text"></label>Demo: https://codesandbox.io/s/pk6k50qm1m
Even if that was supported in REACT 15, I am not sure that it's a good idea to add that flexibility in REACT 16.
It would maybe be more beneficial to add that in the migration notes.
At least that would enforce people to write better HTML 😉
ad1992 commentedon Apr 18, 2018
This is an interesting bug.
Adding global
onClicklistener doesn't fireonChangelistener of thecontrolled checkbox(when it doesn't have a wrapper)Any of the below steps makes it working
on changing
OnClickglobal listener toonChangeglobal listener everything works fine.if
onClicklistener is used in checkbox for the controlled component when adding globalonClicklistener, It works fine.If the third paramter in document,addEventListener which is basically the
useCaptureis set tofalse, everything works finesetStateis commented on the callback of the global event listener then it works fineReaverart commentedon Apr 18, 2018
@stmoreau thank you, but
inputdont have to be nested inlabeland my example is totally valid, you might read it atusage notessection on MDN by link you left. I also suspect that it fixes checkbox only due to nesting, because second checkbox in my example was nested in adivand worked fine. So guess you messing things up.@ad1992 yes, thank you, there is workarounds, the sad part - sometimes you cant control it, like if we have 3rd party UI lib. Assume its wide issue because global
onclickevent listener with capture phase used almost everywhere to capture clicks outside elements (popovers, datepickers, selects and etc).jquense commentedon Apr 18, 2018
this seems like a bug to me. I think #10830 should fix it
stephenkingsley commentedon May 10, 2018
In this case, maybe the checkbox that id is checkbox can not fire onChange event. @jquense I do not know your fix, could you please explain for me?
stvkoch commentedon May 10, 2018
Humm... You'll see that your example in React 16 will work well when you configure addEventListener to trigger callback in bubbling phase
document.addEventListener("click", this.handleIncrement, false);But still be a bug because the capture phase flag only set in what direction should be propagate the event.
[-]16 version breaks checkbox onChange[/-][+]Checkbox doesn't fire onChange in the presence of a capture-phase document click listener[/+]gaearon commentedon Aug 16, 2018
Wow. This is an interesting bug. Digging into it.
Here's a minimal repro:
[-]Checkbox doesn't fire onChange in the presence of a capture-phase document click listener[/-][+]Checkbox doesn't fire onChange in the presence of a capture-phase document click listener that calls setState()[/+]gaearon commentedon Aug 17, 2018
While #10830 would fix it, I don't think it really solves the underlying problem which has to do with the input tracking mechanism.
Here's a repro case that only uses regular inputs, not checkboxes:
Typing doesn't work — unless I comment out that
setStatecall in the capture phase listener.Say the input is empty and we're typing
a.What happens here is that
setState({})in the capture phase non-React listener runs first. When re-rendering due to that first emptysetState({}), input props still contain the old value ("") while the DOM node's value is new ("a"). They're not equal, so we'll set the DOM node value to""(according to the props) and remember""as the current value.Then,
ChangeEventPlugintries to decide whether to emit a change event. It asks the tracker whether the value has changed. The tracker compares the presumably "new"node.value(it's""— we've just set it earlier!) with thelastValueit has stored (also""— and also just updated). No changes!Our
"a"update is lost. We never get the change event, and never actually get a chance to set the correct state.[-]Checkbox doesn't fire onChange in the presence of a capture-phase document click listener that calls setState()[/-][+]onChange doesn't fire if a capture-phase document listener for the underlying native event calls setState()[/+]gaearon commentedon Aug 17, 2018
I can reproduce this going as far back as... React 0.11.
😄
gaearon commentedon Aug 17, 2018
There does seem to be a difference in 16 and 15 behavior though. I'm not sure if it's limited to checkboxes or other inputs too. Maybe it's between
setStatebeing in same or different components.gaearon commentedon Aug 17, 2018
In particular I don't understand why in this example we end up committing
inputupdate at all, or even reconciling it (we get into the complete phase). I'd expect it to bail out becausesetStatewas in theCounter. Or maybe our bail out doesn't prevent complete phase from running? That would explain why wrapping in adiv"fixes" it. Seems like that's a more generic problem that we can fix.gaearon commentedon Aug 17, 2018
I split this out into two issues.
gaearon commentedon Aug 17, 2018
The particular regression in 16 was fixed via #13423 so you'll get the fix in the next patch.