mercredi 30 juin 2021

How to remove a string from an array based on string matching id

I'm currently rendering a Checkbox where if checked, the data (id in a string) is saved in state. However I'm trying to put together the functionality for if the checkbox is unchecked, the data needs to be removed from the state (which is an array of strings).

I have come up with this solution however I don't think I am using .splice correctly or if that's the best option for me to use as it doesn't work.

<Checkbox
        onChange={(e) => {
          e.target.checked
            ? setSelectedStyles([...selectedStyles, s.id])
            : e.target.checked === false &&
              selectedStyles.map((id) => id === s.id)
            ? selectedStyles.splice(s.id)
            : console.log(`didn't work`)
        }}
      ></Checkbox>

To clarify, selectedStyles is an array of strings stored in state. s.id is the id (string) I'm checking against.

Does anyone have any thoughts on how to approach this?

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire