-1

i want to add or remove value from the array. when i select the checkbox the value will pushed to array. when i unselect the checkbox the value will remove from the array.

CheckBox

<CheckBox
 checked={this.state.currentValue}
onClick={() => this._changeValue(index)}>

onClick function

_changeValue(value) {
    this.setState({
      currentValue: !this.state.currentValue,
    });
   console.log(value)
   selectedQuesiton.push(value)
  }
Yash Vaghela
  • 562
  • 1
  • 5
  • 16

1 Answers1

0

this is how i am doing

_changeValue(value) {
   this.setState({
     currentValue: !this.state.currentValue,
   });
   if(selectedQuesiton.includes(value)){
       selectedQuesiton = selectedQuesiton.filter(item => item !== value)
       }
       else{
         selectedQuesiton.push(value)
       }
       console.log(selectedQuesiton)
 }

i am using filter to check data is available in the array or not and if it is then it will just ignore that item in to new array which selectedQuesiton in your case

hope this works and if it does not then comment me

chetan godiya
  • 286
  • 2
  • 13