Bug: `preventDefault()` breaks checkboxes, they may not reflect the `checked` prop
See original GitHub issueReact version: 18.2.0
Steps To Reproduce
- call
event.preventDefault()
inonChange
oronClick
handler - the checkbox element may not reflect the
checked={checked}
prop in some cases
Link to code example: https://codesandbox.io/s/relaxed-taussig-ypxiuo?file=/src/App.js
The current behavior
I can’t click on <input type="checkbox" />
reliably anymore.
It’s preventing us from adopting React 18 in a project, as some of our tests are broken by this bug.
The expected behavior
React 18 should always respect the checked={checked}
prop.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:11 (1 by maintainers)
Top Results From Across the Web
react checkbox: event.preventDefault() breaks onChange ...
It does not indicate whether this checkbox is currently checked: if the checkbox's state is changed, this content attribute does not reflect ......
Read more >event.preventDefault does not always prevent what you might ...
When it comes to React, on each rerender, the checked attribute will reflect the current state, but it's still only a default value...
Read more >Checkbox not working - Inertia with ReactJS - Laracasts
When I try to check the roles and output the data in the console, I get the error: Warning: A component is changing...
Read more >Changelog - Shoelace
Components with the Experimental badge should not be used in production. ... checked icons to not scale property when resized; Fixed a bug...
Read more >HTML DOM Input Checkbox disabled Property - W3Schools
A disabled element is unusable and un-clickable. Disabled elements are usually rendered in gray by default in browsers. This property reflects the HTML...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think you are restricting the default behavior of
checkbox
by addingpreventDefault()
resulting the different behavior.Hi there,
Try
event.stopPropagation()