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.

Working with checkboxes always produces warning in console

See original GitHub issue

To reproduce the problem add something like this in a kotlin-react app. The state needs to have a var myBoolean, that has an initial value of false:

input(InputType.checkBox) {
  attrs.onChangeFunction = { event ->
    val newValue = (event.target as HTMLInputElement).checked
    setState { myBoolean=newValue }
  }
  attrs.checked = state.myBoolean
}

Then open the app in the browser and click on the checkbox. The following warning will appear in the console of the browser: “A component is changing an uncontrolled input of type checkbox to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component.”

I have investigated the issue myself and I suspect that the problem is due to the setter of the checked property of the input removing the checked attribute when the value is false.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
fluidsoniccommented, Nov 7, 2020

I also ran into this.

Workaround:

val isChecked: Boolean = …
// attrs.checked = isChecked
attrs["checked"] = isChecked
1reaction
TorRanfeltcommented, Aug 25, 2018

@Hypnosphi Sadly, as three developers in a startup we don’t have resources for that kind of thing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Checkboxes in ReactJS - Stack Overflow
I found this question but it has a ton of upvoted answers where everyone is doing something else. Some answers use checked={isCheckedState} , ......
Read more >
How to work with checkboxes in React - DEV Community ‍ ‍
Now if you try to check the checkbox, nothing would happen and you will see the following warning in the console:.
Read more >
<input type="checkbox"> - HTML: HyperText Markup Language
Checkboxes do support validation (offered to all <input> s). However, most of the ValidityState s will always be false . If the checkbox...
Read more >
Check if a Checkbox is checked in React | bobbyhadz
Use the `target.checked` property on the `event` object to check if a checkbox is checked in React, e.g. `if (event.target.checked) {}`.
Read more >
Checkbox not working - Inertia with ReactJS - Laracasts
I have a form in a modal that creates a user with roles. When I try to check the roles and output the...
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