-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
event.preventDefault in click handler does not prevent onChange from being called #9023
Comments
The This plugin is trying to figure out if the value of the checkbox will change, and if yes it will queue the The The problem is that the
and on the next click on the checkbox, the
Overall the problem seems to be that React is asking the DOM what the Thanks @karelskopek and @Mintaffee for your help with digging into this. |
@aweary Do you think this is worth solving? |
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.. |
Is this related? |
Still an issue in React 16.8.6. One possible workaround is to do everything in const radios = ['one', 'two', 'three'];
class Row extends Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
this.onChange = this.onChange.bind(this);
}
render() {
const { value } = this.props;
return (
<div className="row">
<div className="radio-group">
{radios.map(radio => (
<label key={radio} className="radio">
<input
type="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 change
if (...) {
event.preventDefault();
return;
}
// Sync value in the store
this.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 change
if (...) {
return;
}
// Sync value in the store
this.props.setDateTime(event.target.value);
} |
For a simpler solution, you can do e.preventDefault on onMouseDown event to prevent onChange from firing.
|
WARNING: Only tested in React 17 + Chrome 87 As a workaround you can wrap the built-in function Checkbox(props) {
const { onChange, ...other } = props;
function handleChange(event) {
if (event.nativeEvent.defaultPrevented === false) {
onChange(event);
}
}
return <input {...other} onChange={handleChange} type="checkbox" />;
} -- https://codesandbox.io/s/checkbox-click-preventdefault-change-quirk-usljx Original source: mui/material-ui#23709 (comment) |
Do you want to request a feature or report a bug?
Bug!
What is the current behavior?
When rendering an
input
element of typecheckbox
with anonClick
andonChange
handler,onChange
is still called even thoughevent.preventDefault()
is called in theonClick
handler.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/reactjs/69z2wepo/).
http://jsfiddle.net/rf3w7apc/
What is the expected behavior?
Calling
event.preventDefault
in theonClick
handler should prevent the default action from occurring (or undo its effect), which is to update the value of theinput
element. This should stop anychange
event listener from being invoked. See https://jsfiddle.net/L1eskzsq/ for expected behaviorWhich versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Tested using a build from master, macOS 10.12.2, verified in:
Safari 10.0.2 calls the
change
event listener in both cases.The text was updated successfully, but these errors were encountered: