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.

input checkbox not updating after re-render

See original GitHub issue

React version: 16.4.2

The checkbox in the DOM is not checked after a state change.

constructor(props) {
    super(props);

    this.state = {
        checked: false
    }

componentDidMount() {
    this.setState({
        checked: this.props.checked
    });
}

render() {
    let checked = <input type='checkbox' onChange={() => {
        this.setState({checked: !this.state.checked});
    }} defaultChecked={this.state.checked} />

    return (
         // JSX
     )
}

Console logging checked, the first render returns a react.element type: 'input" with the following props

props:
    defaultChecked: false

Second render after state change

props:
    defaultChecked: true

However, in my app, the checkbox is not checked. If I explicit set the defaultChecked property to true, then it will be checked. If I default the state.checked to true, it will be checked. I’ve tried removing the onChange listener, suspecting that that may be the culprit, but it’s not.

My current workaround

constructor(props) {
    super(props);

    if (this.props.checked) {
        this.state = {
             checked: true
        }
    } else {
        this.state = {
             checked: false
        }
    }
}

Feels messy.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:24 (3 by maintainers)

github_iconTop GitHub Comments

26reactions
a1theredbullcommented, May 4, 2019

Also getting the same issue with checked attribute set to state, state updating, and not updating DOM. The DOM updates (reflecting the new checked value) as soon as I change anything else in the state. This happens on my project for checkbox and radio input.

I’m using a fully controlled input in both cases. The state variable is updated in React dev tools, and I also printed out the updated value before render. It just seems the DOM won’t update in this particular case.

Unfortunately I couldn’t repro this on codepen so there must be some way my project is set up (along with the other people reporting this) that is triggering this effect.

UPDATE I figured out that I had a parent component that had an onClick that was calling evt.preventDefault(). I removed that call and the checkbox and radio inputs now update in the DOM when state is updated. I’m not sure why preventDefault would prevent a render of the DOM in this specific case, but I’m also fairly new to React and don’t know the inner workings. Is anyone able to shed some light on this?

Here’s a Code Sandbox demonstrating the issue: https://codesandbox.io/s/v67qwj1870

18reactions
julianCastcommented, May 23, 2019

For those with this problem, as @a1theredbull said, do not include preventDefault on the change function:

function onCheckedChanged(e)
  {
    //e.preventDefault();  <-- Remove this, it will mute the event.
    folder.setSelected(e.target.checked);
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Checkbox Does Not Update - Stack Overflow
Show activity on this post. I want to update an array each time a checkbox is toggled to true. With this current code,...
Read more >
How to Control a Checkbox with React Hooks - Medium
The setChecked function updates the state. It can take a new state value and update the state. Updating the state triggers a rerender...
Read more >
Checkboxes In React.js
when i clicked in checkbox than getting below warning. A component is changing an uncontrolled input of type checkbox to be controlled. Input...
Read more >
[Solved]-React Checkbox Does Not Update-Reactjs
setState() enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated...
Read more >
FAQs | React Hook Form - Simple React forms validation
Why is default value not changing correctly with ternary operator? ... React Hook Form doesn't control your entire form and inputs, which is...
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