React a component is changing an uncontrolled input of type checkbox to be controlled

react gives me a warning: “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).”

However my checbox is change via the state property. Am I missing something obvious?

    import React from 'react';

// Components
import Checkbox from './checkbox';
import HelpBubble from './helpBubble';

export default class CheckboxField extends React.Component {


    constructor(props) {
        super(props);
        this.state = {value: props.value};
        this.handleChange = this.handleChange.bind(this);
    }

    handleChange(event) {
        this.setState({value: event.target.value});
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.value !== this.props.value) {
            this.setState({value: nextProps.value});
        }
    }

    render() {
        const {label, meta = {}, help, disabled, required, onChange} = this.props;

        return (
            <label className="checkbox-wrap form-field">
                <Checkbox
                    disabled={disabled}
                    type="checkbox"
                    onChange={(event) => {
                        onChange(event, !this.state.value);
                    }}
                    checked={this.state.value}/>
                {label && (
                    <div className="checkbox-label">
                        {label}
                        {required && <div className="form-field__required"/>}
                    </div>
                )}
                {help && <HelpBubble help={help}/>}
                {meta.error && meta.touched && (
                    <div className="input-error">{meta.error}</div>
                )}
            </label>
        );
    }}

Parent component:

handleChangeParams(key, value)
}
/>
Handle change params changes the value in model and calls server. Depending on server result, the value can change.

Thanks in advance.

If your state is initialized with props.value being null React will consider your Checkbox component to be uncontrolled.

Try setting your initial state so that value is never null.

this.state = { value: props.value || "" };

If you are using a checkbox react won’t like a string either so instead try

this.state = { checkboxValue: props.checkboxValue || false };

Something worth noting about the above code snippet. When you set a state in the constructor from props, it is always best to set the state to a “controlled” value i.e. a tangible value such as an int, float, string, array, map, etc. The error you are getting is the result of props.value being set to null

Read More:   Multi-line string insert using jQuery

So, Consider setting your constructor state like this:

this.state = {
    value: props.value ? props.value : 'empty'
}

What is happening here is it is checking if props.value has a value, if it does it sets the state to props.value, if props.value is null, it sets the state to the string: `’empty’

Another simple way to do this would be to !! your props.checkboxValue value. That way even if it’s undefined, !!props.checkboxValue will resolve to false.

this.state = { checkboxValue: !!props.checkboxValue };

In my case, I was using a prop from my redux store to set whether the checkbox was checked, simply defaulting the property to false worked for me.

e.g.

const MyComponent = ({
  somePropFromRedux
}) => {
  return <thatThridPartyCheckboxComponent checked={somePropFromRedux} />
}

becomes (only change is adding = false on Line 2)

const MyComponent = ({
  somePropFromRedux = false
}) => {
  return <thatThridPartyCheckboxComponent checked={somePropFromRedux} />
}

do not use e.target.checked in the inputbox onChange eventHandler method.

Correct way:

  const [isChecked, setCheck] = useState(false);
 const handler = (e) => {
    setCheck(!isChecked);
  };

 <input
        type="checkbox"
        checked={isChecked}
        onChange={handler}
      />


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts