react-tag-autocomplete handling duplicate tags

Viewed 434

I'm using react-tag-autocomplete Documentation.

Everything works well but when I try to remove duplicate values from custom input(triggered by allowNew) the duplicates are not removed.

Try inputting custom tags using enter or tab.

Here is an example on codesandbox

Edit nice-ramanujan-brydo

1 Answers

The includes method does not works for objects.

You could use map method for collecting the all names by passing a callback function as argument.

handleAddition(tag) {
    var names = this.state.tags.map(({name}) => name);
    if (!names.includes(tag.name)) {
        this.setState(state => ({ tags: [...state.tags, tag] }));
    }
}
Related