I am creating a simple list of cards and I need to filter it according to which tags are selected. I have:
function Homepage() {
const [cards, setCards] = useState([]);
const [filteredCards, setFilteredCards] = useState([]);
let filteredTags = [];
The tags are buttons with onClick={toggleSingleTag}
and then I have this function:
function toggleSingleTag(e) {
if (!filteredTags.includes(e.target.innerHTML)){
filteredTags.push(e.target.innerHTML);
} else {
filteredTags = filteredTags.filter(function(elem){
return elem != e.target.innerHTML;
})
}
// setFilteredCards(filteredTags)
}
If I console.log filteredTags at the end of the function, I get the expected result (tags are included or removed from the list).
But when I try to add setFilteredCards()
(removing comment on the line) and console.log the values, I get that:
- filteredTags only ever includes the last clicked item (n), regardless of what was clicked at n-1. So it looks like the list is emptied everytime before the function is triggered, otherwise if n == n-1 the list should be empty.
- filteredCards includes the n-1 element and nothing else.
What am I doing wrong? How do I fix this?