mardi 26 février 2019

Material UI checkbox complaining to be uncontrolled

I'm using Material UI kit for React. I'm making Checkboxes dynamically from states and updating them,

But I'm getting uncontrolled element error.

this.state = {
    services : [{service: "s1", value: 1},
                {service: "s2", value: 2},
                {service: "s3", value: 3},
               ]
};

handleServiceCheck = (i) => {
    let services = this.state.services;
    services[i].value = !services[i].value;
    this.setState({ services: services });
};

this.state.services.map((service, i) => (
    <FormControlLabel key={i}
        control={
            <Checkbox
                checked={service.value}
                onChange={() => this.handleServiceCheck(i)}
                value={service.service}
                className={classes.checkBox}
            />
        }
        label={service.service}
    />
))

Uncontrolled Input Element




Aucun commentaire:

Enregistrer un commentaire