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
event.preventDefault in click handler does not prevent onChange from being called #9023
When rendering an input element of type checkbox with an onClick and onChange handler, onChange is still called even though event.preventDefault() is called in the onClick handler.
Overall the problem seems to be that React is asking the DOM what the checked value of the node is, before the preventDefault in onClick could be called.
No progress with this one? I'm using React v16.2 and the behaviour is the same - onChange is triggered first time and subsequent clicks doesn't trigger it. I would say quite high priority issue here..
Still an issue in React 16.8.6. One possible workaround is to do everything in onClick handler:
constradios=['one','two','three'];classRowextendsComponent{constructor(props){super(props);this.onClick=this.onClick.bind(this);this.onChange=this.onChange.bind(this);}render(){const{ value }=this.props;return(<divclassName="row"><divclassName="radio-group">{radios.map(radio=>(<labelkey={radio}className="radio"><inputtype="radio"name="radio"value={radio}checked={radio===value}onClick={this.onClick}onChange={this.onChange}/><span>Radio</span></label>))}</div></div>);}onClick(event){// Check something to prevent radio changeif(...){
event.preventDefault();return;}// Sync value in the storethis.props.setValue(event.target.value);}onChange(){// We need this empty handler to suppress React warning:// "You provided a `checked` prop to a form field without an `onChange` handler.// This will render a read-only field. If the field should be mutable use `defaultChecked`.// Otherwise, set either `onChange` or `readOnly`"}}
If you use controlled component another option I found is to simply leave value in the state unchanged:
onChange(event){// Check something to prevent radio changeif(...){return;}// Sync value in the storethis.props.setDateTime(event.target.value);}
Activity
hejld commentedon Feb 18, 2017
The
changeevent is being created by theChangeEventPluginas a response totopChange, before youronClickhandler is even called (so before thepreventDefaultcall), here: https://github.com/facebook/react/blob/master/src/renderers/dom/shared/eventPlugins/ChangeEventPlugin.js#L338This plugin is trying to figure out if the value of the checkbox will change, and if yes it will queue the
changeevent. It uses theinputValueTrackingfunctionality to compare the previous value with the next value to make this decision, here: https://github.com/facebook/react/blob/master/src/renderers/dom/shared/inputValueTracking.js#L154The
nextValueis take directly from the DOM like this:value = isCheckable(node) ? '' + node.checked : node.value;https://github.com/facebook/react/blob/master/src/renderers/dom/shared/inputValueTracking.js#L56The problem is that the
node.checkedvalue is temporarily true at this point, so after the first click it is:lastValue == false(correct)nextValue == true(incorrect)and on the next click on the checkbox, the
changeevent is no longer being fired, because the values are:lastValue == true(incorrect)nextValue == true(incorrect)Overall the problem seems to be that React is asking the DOM what the
checkedvalue of the node is, before the preventDefault inonClickcould be called.Thanks @karelskopek and @Mintaffee for your help with digging into this.
hejld commentedon Mar 12, 2017
@aweary Do you think this is worth solving?
powah commentedon Mar 21, 2018
No progress with this one? I'm using React v16.2 and the behaviour is the same - onChange is triggered first time and subsequent clicks doesn't trigger it. I would say quite high priority issue here..
maddie927 commentedon Feb 21, 2019
Is this related?
preventDefaultcalled in a checkboxonChangecauses React and the browser state to go out of sync: https://codesandbox.io/s/0qpr936xkvezze commentedon Apr 19, 2019
Still an issue in React 16.8.6. One possible workaround is to do everything in
onClickhandler:If you use controlled component another option I found is to simply leave value in the state unchanged:
ronn9419 commentedon Oct 21, 2020
For a simpler solution, you can do e.preventDefault on onMouseDown event to prevent onChange from firing.
onMouseDown(e) { if (...) { e.preventDefault(); return; } }eps1lon commentedon Nov 30, 2020
WARNING: Only tested in React 17 + Chrome 87
As a workaround you can wrap the built-in
inputcomponent with a custom one that checks forevent.nativeEvent.defaultPreventedlike so:-- https://codesandbox.io/s/checkbox-click-preventdefault-change-quirk-usljx
Original source: mui/material-ui#23709 (comment)