Answer the question
In order to leave comments, you need to log in
Why doesn't condition work in JSX?
There is a function that, depending on the condition, renders a certain array of colors, depending on the selected category. The conditions are simple - if such and such props === true, then the function should work and display the required set. But doesn't work.
Here is the function:
displayColors = (colorSet) => {
colorSet.map((color, index) => {
return (
<li className={"filter-item " + (this.state.color === color ? "active-item" : null)} key={index}
onClick={() => this.handleFilter("color", color)}> {color} </li>
)
})
}
handleFilter = (name, value) => {
this.setState ({
[name]: value
}, this.sortData)
}
<ul className = {"filter-list colors-list " + (colorFilter ? "color-container-visible" : null)}>
{(categoryOneColors ? displayColors(colorSet) : null )}
{(categoryTwoColors ? displayColors(colorSet2) : null )}
...
...
</ul>
Answer the question
In order to leave comments, you need to log in
The problem is that displayColors does not return the generated array. It should be like this:
displayColors = (colorSet) => {
return colorSet.map((color, index) => (
<li
className={`filter-item ${this.state.color === color ? 'active-item' : null}`}
key={index}
onClick={() => this.handleFilter('color', color)}
> {color} </li>
));
}
displayColors = (colorSet) => colorSet.map((color, index) => (
<li
className={`filter-item ${this.state.color === color ? 'active-item' : null}`}
key={index}
onClick={() => this.handleFilter('color', color)}
> {color} </li>
))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question