Answer the question
In order to leave comments, you need to log in
Why does the value of the state object not change?
There is state
, and this one state
is an object.
state = {
productToCart: {
size: '',
color: ''
}
}
state
. {itemSizes.map((size, index) => {
return (
<li key={index} className={"filter-item " + (this.state.productToCart.size === size ? "filter-active" : null)} onClick={() =>
{this.pickSize(size)}}> {size} </li>
)
}
)}
...
{itemColors.map((color, index) => {
return (
<li key={index} className={"filter-item " + (this.state.productToCart.color === color ? "filter-active" : null)} onClick={() =>
{this.pickColor(color)}}> {color} </li>
)
}
)}
pickSize = (size) => {
this.setState({
productToCart: {
size: size
}
})
}
...
pickColor = (color) => {
this.setState({
productToCart: {
color: color
}
})
}
state
, but not both at the same time, because the previous value is replaced by a new one. I clicked on the size - it shows only the size, on the color - only the selected color is displayed. Why are both the first and the second not displayed at the same time, because the functions change, in fact, different values and do not interact with each other in any way?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question