I have a component to display list of tags, and the user can select tags to follow them. So when the user clicks a tag, I would like to store it in the tagsSelectedList and add a new class name selected_tag. But if the the tag is already present in the tagsSelectedList, remove the tag from the tagsSelectedList and also remove the class name selected_tag.
I am confused as how to check if the tag object is present inside tagSelectedList and remove that specific tag item from the list. How do I do that? Your help and guidance will be much appreciated. Thank you.
component.js:
let tags = [
{id: "1", tagName: "Arts"},
...
...
{id: "59", tagName: "Writing"}
];
var tagsSelectedList = [];
export default class SignUpSubscribeTags extends React.Component {
constructor(props){
super(props);
}
selectTag = (e) => {
// if the tag is not present in the tagsSelectedList then add the tag in the tagsSelectedList also add the tag_selected class name
var isTagPresent = tagsSelectedList.filter(function(item) {
return item.id === e.target.dataset.pk
});
if(isTagPresent) {
console.log(e.target.dataset.pk, 'present in tagsSelectedList');
}
else {
console.log(e.target.dataset.pk, 'not present in tagsSelectedList');
}
// if the tag is present in tagsSelectedList then remove the tag object from the tagsSelectedList also remove the tag_selected class name
}
render() {
let tagList = tags.map((Tag) => {
return (
<li id={Tag.tagName} class="tag" data-pk={ Tag.id } key={Tag.id} onClick={this.selectTag}>{Tag.tagName}</li>
);
});
return(
<div id="signup-process-wrapper-addTags">
<div id="add_tags">
<ul id="tag_list">
{tagList}
</ul>
</div>
</div>
);
}
}