This is a very straight forward operation but for some reason the code does not add and remove items from the array as it should.
var [topicList, setTopicList] = React.useState([chipData[0].label]);
function addTopic(data) {
if (topicList.indexOf(data.label) > -1) {
setTopicList(topicList => topicList.filter(topic => topic !== data.label));
} else {
setTopicList(topicList => [...topicList, data.label]);
}
// Toggle chip UI
chipData[data.key] = {key: data.key, label: data.label, flag: !data.flag};
setChipData([...chipData]);
console.log(topicList);
}
I would like to add data to the topicList
if it's not in the array and remove otherwise.