1

I have this code that inserts the ids of the checkboks into an array, but if I disable them, it does not remove them, i use update to react-addons-update any suggestions

     constructor(props) {
        super(props);
         this.state={
           keyGen:[]
         }
      }
      render(){  
         <form> 
            {this.renderElements()}
             <input type="submit" value="Save" />
        </form>
      renderElement(){
       return this.props.Elements.map((item, index)=>{
render(
        <Input name='list' type='checkbox' onClick={()=>this.updateStateList(item.id)} label='Add' className='listTour' />
       ) 
        })
        }

      updateStateList(value){
        this.setState(update(this.state, {keyGen: {$push:[value]}}))
        console.log(this.state.keyGen)
      }

thanks

5
  • if you uncheck the checkbox, it doesn't remove the items from the array? Commented Jan 26, 2017 at 2:29
  • no, only insert items Commented Jan 26, 2017 at 2:29
  • I don't understand your question. What do you mean by "if I disable them"? Commented Jan 26, 2017 at 2:30
  • The idea is that when doing uncheck, remove the id from that checkbocks in the array Commented Jan 26, 2017 at 2:32
  • Just use array.filter() in the updateStateList method to return the array without the Id. First you check if the Id that was just clicked is present in the array via array.some(). If it is present, use array.filter() to filter the array out and set keyGen equal to the filtered array. Commented Jan 26, 2017 at 2:36

1 Answer 1

5

// modify render function like this.

You can see the working code here http://codepen.io/umgupta/pen/dNVoBg See console tab at the bottom

    class UpdateStateDemo extends React.Component {
      constructor(props) {
        super(props);
        this.state={keyGen:[]} 
      }

      render(){ 
        console.log(this.state)
        return (
        <form>
            {
              this.props.Elements.map((item, index)=>{  
                return (
                  <input 
                    key={item} 
                    name='list' 
                    type='checkbox' 
                    onClick={(e)=>this.updateStateList(e,item)}
                    label='Add' 
                    className='listTour' />
                ) 
             })  
            }
          <input type="submit" value="Save" />
        </form>
        )
      }

  updateStateList(e, value){
    console.log(e.target.checked)
    if (e.target.checked){
      //append to array
      this.setState({
        keyGen: this.state.keyGen.concat([value])
      })
    } else {
      //remove from array
      this.setState({
        keyGen : this.state.keyGen.filter(function(val) {return val!==value})
      })
   }
}
}
9
  • It works thanks, but it's funny why when I do the first select it shows me the empty array you know why it can be Commented Jan 26, 2017 at 2:51
  • Because you have an empty array set in state? Commented Jan 26, 2017 at 2:52
  • But if the user sends only one data, then send the empty array, how could solve it Commented Jan 26, 2017 at 2:57
  • 1
    I tried many different stack overflow posts and solutions, I felt I could understand nearly all of them, but none of the them worked for, none of them removed the value from the arrary, but yours works, and its also simple cheers Commented Jan 10, 2019 at 0:01
  • 1
    @AndrewIrwin Thanks (especially for the appreciation)! I am glad that you found it helpful. Commented Jan 10, 2019 at 2:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.