I want the user to tap on an item from a list and add it to their favorites page. The problem I’m having is that if the user taps on the same item twice it will be added to their favorites page twice and create issues because two children have the same key.
I’m updating the state this way in reducer:
const initialState = {
selectedFavorites: [],
};
addFavorite: (state, action) => {
return {
...state,
selectedFavorites: [...state.selectedFavorites, action.payload],
};
},
When user taps on item to be added to favorites:
const handlePress = () => {
favorite === true ? setFavorite(false) : setFavorite(true);
dispatch(
addFavorite({
id: currentIndex,
title: getIds[currentIndex].title,
liked: 'true',
}),
);
setTimeout(() => {
setFavorite(false);
}, 500);
};
I’ve tried using new Set()
but was unable to make this work. What is a good way to copy and render the state without any duplicate values?
3
Answers
Instead of fixing this in the reducer, I’d fix it before the
dispatch
, for example, check if it’s already a favourite.Assuming
ids
contains a list of all favourite indexes, useincludes
to check if this ID already exists in the arrayIf what you want is way of filteing only distinct values of a list, this solution, written by @TLindig.
But you also could check if the item is already favorited before calling dispatch:
In the
addFavorite
reducer function check if theselectedFavorites
array already contains an element with matching id value, and only add new favorites.Example:
Alternatively it appears you could fix this in the handler since it seems you also keep and maintain a local
favorite
state.Example: