How can I add an if else
condition inside tags.map
ternary operator to check below condition ? Do I need change the existing ternary operator to achieve this ? Could someone please help ?
Demo link: https://codesandbox.io/s/prod-glade-lsf46?file=/src/App.js
if(findTag == tag) {
<a onClick={getFilterTags} className="grouptechtags" style={{backgroundColor: `${showColor}`},{ marginRight: "10px" }} key={tag}>{tag}</a>
} else {
}
// Following is the code
const [posts, isLoading] = usePosts();
const [searchResults, setSearchResults] = useState([]);
const [showColor, setShowColor] = useState("");
const [findTag, setFindTag] = useState("");
const randomizedHex = (tags) => {
setFindTag(tags);
console.log("Print tag of a:"+tags);
const randomColor = "#"+ Math.floor(Math.random()*16777215).toString(16);
setShowColor(randomColor);
}
<label>
{
searchResults.map(({ fields: { id, tags } }) => (
<div key={id} className="techtags">
{
Array.isArray(tags) ? (
tags.map((tag) => (
<a onClick={getFilterTags} className="grouptechtags" style={{backgroundColor: `${showColor}`},{ marginRight: "10px" }} key={tag}>{tag}</a>
))
) : (
<a onClick={getFilterTags} style={{backgroundColor: `${showColor}`}} className="grouptechtags">{tags}</a>
)
}
</div>
))
}
</label>