ReactJs multiples checkbox using Material

Viewed 24

I have a problem retrieving the values selected in the checkbox I have a list with several names but when I select a name in the checkbox it is selected. However, when trying to select a different name, the value retrieved is always the same as the first selection.

const onNameChange = (event)=>{
        let value = state.name.split(";");
        if(value.includes(event.target.value)){
            let index = value.indexOf(event.target.value);
            value.splice(index, 1);
        }else{
            value.push(event.target.value);
        }
        if(value[0] === ""){
            value.splice(0, 1);
        }
        value = value.join(";");
        setState({
            ...state,
            name: value
        });
    }

<label className="label-value">
                    {bridge.parent.arrayNames.map((element, index) => (
                        <label key={index.key}>
                            {element.name}
                            <Checkbox value={element.name}
                                    onChange={onNameChange}/>
                        </label>
                    ))}
                </label>
0 Answers
Related