I have following tag selection:
It works fine but the problem is it hangs for a few seconds after I'm able to select another Tag. This is my code:
let selected = []
const TagScreen = () => {
const [allInterests, setAllInterests] = useState(["interst1", "interst2", "interst3", "interst4", "interst5", "interst6", "interst7", "interst8", "interst9", "interst10", ...
const [interests, setInterests] = useState([])
const fire1 = (item) => {
selected.unshift(item)
setInterests([...interests, item])
}
const fire2 = (item) => {
let index = selected.indexOf(item);
setInterests([...interests, item])
if(index!=-1){
selected.splice(index, 1);
}
}
return (
<View>
<View style={{display: "flex", flexWrap: "wrap", flexDirection: "row"}}>
{allInterests.map((item) => {
return(
<View key={Math.random()}>
{!selected.includes(item) ?
<PressableBackground onPress={() => !selected.includes(item) ? fire1(item) : fire2(item)} >
<View style={{margin: 2.5,}}>
<Tag>
<Text style={{color: colors.text}}>{item}</Text>
</Tag>
</View>
</PressableBackground>
:
<PressableBackground onPress={() => !selected.includes(item) ? fire1(item) : fire2(item)} >
<View style={{margin: 2.5,}}>
<Tag color={colors.card}>
<Text style={{color: colors.background}}>{item}</Text>
</Tag>
</View>
</PressableBackground>
}
</View>
)
})}
</View>
</View>
);
}
export default TagScreen;
I also tried React.Callback or React.useMemo. Sadly it didn't work