question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: `preventDefault()` breaks checkboxes, they may not reflect the `checked` prop

See original GitHub issue

React version: 18.2.0

Steps To Reproduce

  1. call event.preventDefault() in onChange or onClick handler
  2. 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

Recording 2022-08-09 at 18 23 20

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:open
  • Created a year ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
SivaKrishna6745commented, Aug 11, 2022

I think you are restricting the default behavior of checkbox by adding preventDefault() resulting the different behavior.

2reactions
goldmontcommented, Aug 10, 2022

Hi there,

Try event.stopPropagation()

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found