I'm trying to mark divs that is clicked on my website. When I click, the array is updated but the mark won't show. It seems like the statement gameChoices.includes('Fortnite') is false, even though the array contains the exact value Fortnite.
Does anyone know why this happens? Eventually a new solution for the problem?
Code:
<Container onClick={() => {
if (gameChoices.includes('Fortnite')) {
const findIndex = gameChoices.findIndex(a => a === 'Fortnite')
findIndex !== -1 && gameChoices.splice(findIndex , 1)
} else if (gameChoices.includes('Fortnite') === false) {
gameChoices.push('Fortnite')
}
}} fluid className="d-flex fortnite gameoption position-relative">
{gameChoices.includes('Fortnite') ?
<>
<BsCheckSquare color="lightgreen" size="2rem" style={{ top: '50%', right: '50%' }} />
</>
: null
}
<h1 className="fw-bolder text-light text-center m-auto">FORTNITE</h1>
</Container>
const [gameChoices, setGameChoices] = useState([])

findIndex !== -1is not required as you are already checking if it is included in array.gameChoices.includes('Fortnite') === falseis redundant. Just a simple else is enoughgameChoisesprobably isn't a state variable, update your code to make it a state with theuseState()hook