lundi 27 juillet 2020

React Native checkbox element is not working

I am using checkbox from react-native-elements. I am facing issue with checkbox checked value.

First I have to get data from database. I am getting preference array with two key and values such as:

  1. name
  2. Key

for an instance: {name:'Events', selected:'true'}

Now, I am retrieving name value from database and setting that name for my checkbox, it is working fine. I am having issue with checkbox checked button.

Here is my checkbox :

 <CheckBox
 key={index}
 name={x.name}
checked={this.state.checked}
checkedIcon={<Image source={require('../../assets/icons/checkmark.png')} style= />}
uncheckedIcon={<Image source={require('../../assets/icons/check-box.png')} style= />}
onPress={() => this.toggleCheckbox(x.name)}/>

Here is my toggleCheckbox method:

toggleCheckbox(name) {

    console.log("NAME===", name);

    const changeCheckbox = this.state.filterCategoryName.find((cb) => cb.name === name);
    changeCheckbox.selected = !changeCheckbox.selected;

    if (changeCheckbox.selected) {
        console.log("Checked  if ====", changeCheckbox.selected);
        this.setState({ checked: changeCheckbox.selected })
        console.log("State updating during true====", this.state.checked);
    }
    else {
        console.log("Checked else ====", changeCheckbox.selected);
        this.setState({ checked: changeCheckbox.selected })
        console.log("State updating during false===", this.state.checked);
    }

    const checkboxes = Object.assign({}, this.state.filterCategoryName, changeCheckbox);
    this.setState({ checkboxes });

}

When I select one checkbox at that time it will select all checkbox instead of single and when I uncheck single checkbox it will uncheck all checkbox.

I am not getting id from database only getting name and selected value so I used name in togglecheckmethod.




Aucun commentaire:

Enregistrer un commentaire