lundi 4 décembre 2017

Always get previous state in react app

In relation to my previous question, I'm doing like this to store the right state:

if (value === 'checkpoint')
{
  if (checked1)
  {
    this.setState({checked1 : false})
    localStorage.setObject('checked1', false)
  }
  else
  {
    this.setState({checked1 : true})
    localStorage.setObject('checked1', true)
  }
}

Now I have multiple checkbox, let say I have four. One for ALL checkbox, the other 3 is category checkbox. I want my app to detect when three category checkbox is checked, the ALL checkbox will checked by itself. When either one of the category checkbox is unchecked, the ALL checkbox will unchecked itself.

I tried the below code:

  if (checked1 && checked2 && checked3)
  {
    this.setState({checkedAll: true})
  }
  else
  {
    this.setState({checkedAll: false})
  }

But I'm having problem again because the other 3 checkbox (checked1, checked2, checked3) will always get a previous state.

How to get the right state so that my checkedAll function correctly?




Aucun commentaire:

Enregistrer un commentaire