Add / delete item from state object in React (useState)

Photo by Ferenc Almasi on Unsplash

Test Data

const [tags, setTags] = React.useState({
0: "hello",
1: "world",
2: "hello world"
});

Add Item

const addTag = () => {
setTags({...tags, [Object.keys(tags).length]:"new tag"});
};

Delete Item

const handleClickDeleteTag = (tag) => {
setTags(Object.assign({}, Object.values(tags).filter((filterTag)=>(filterTag !== tag))))
};

πŸ‘» VOICE SOFTWARE CO. πŸ‘» // #Columbus // less thumb-swiping & screen-facing. Wholistic Balance ☯️ of Midwestern Love ❀️ + Artificial Intelligence πŸ’¬