0

I have this stateful variable

const [validMonths2019, setValidMonths2019] = useState([])

Now when the checkboxed is checked the event handler will get called

   <Form.Check label='Januar 2019' id='2019' onChange={(event) => handleCheck('01', event)} />

This is the event handler

  function handleCheck (value, event) {
    const months2019 = [...validMonths2019]
    if (event.target.id === '2019' && event.target.checked) {
      console.log(value) // value is 01
      months2019.push(value)
      setValidMonths2019(months2019)
    } else if (event.target.id === '2019' && !event.target.checked) {
      console.log(value) // value is 01
      months2019.filter(item => item !== value)
      console.log(months2019)// months2019 is still the same array.
      setValidMonths2019(months2019)
    } 
  }

The filter method isn't doing anything. The array stays the same.

1 Answer 1

1

Filter does not mutate the array, instead, it returns a new array so you need to capture it's value

const myNewArray = months2019.filter(item => item !== value)
console.log(myNewArray) //<-- Here is your filtered array
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.