[英]Why do my items change order when I update input value in REACT?
I have simple shopping cart app made with REACT and REDUX.我有使用 REACT 和 REDUX 制作的简单购物车应用程序。
I have some items that are displayed on screen with input filed where ammount of items is changed and based on that new price i calculated.我有一些项目显示在屏幕上,输入字段中的项目数量发生了变化,并且基于我计算的新价格。 But one thing is problematic.
但有一件事是有问题的。
Price is calculated correctly when I update ammout of items or when I add new but but when I update ammount that item is rendered last.当我更新项目数量或添加新项目时,价格计算正确,但当我更新数量时,该项目最后呈现。 So if I update ammount of first item it will just end as last item on the list
因此,如果我更新第一项的数量,它将作为列表中的最后一项结束
1
2
3
4
let's say that I update item 1
then list will look like this假设我更新了
1
项,那么列表将如下所示
2
3
4
1
and this is really frustrating me.这真的让我很沮丧。 This is how I display items
这就是我显示项目的方式
{this.props.cart.map(item => {
return (
<div key={item.product.id}>
<Item item={item}/>
</div>
)
})}
I also tried with ...reverse().map
but it does the same except it puts it on the top of page instead of bottom.我也尝试过
...reverse().map
但它的作用相同,只是它把它放在页面顶部而不是底部。 I want them to stay where they are when they are updated我希望他们在更新时保持原样
This is parent component of single item component.这是单项组件的父组件。 Both of them are class components, I also have this in parent component
它们都是 class 组件,我在父组件中也有这个
const mapStateToProps = (state) => {
return {
cart: state.cart.cart
}
};
export default connect(mapStateToProps)(Cart);
Thank you for your time.感谢您的时间。 If you need any other info just reach out and I will provide it.
如果您需要任何其他信息,请联系我,我会提供。
UPDATE 1 This is how I update cart更新 1这就是我更新购物车的方式
handleChange = e => {
if(e.target.value <= 0) {
alert("Quantity must be grater than 0");
return;
}
if(e.target.value > this.props.item.product.amount) {
alert("Max number of products reached");
return;
}
if(this.state.quantity !== e.target.value) {
this.setState({
quantity: e.target.value,
// btnVisible: true
}, () => this.props.updateCartQuantity(this.props.item.product.id, this.state.quantity))
}
};
<input type="number" className="form-control" onChange={(e) => {this.handleChange(e)}} value={this.state.quantity}/>
This update is happening in child component此更新发生在子组件中
UPDATE 2更新 2
updateCartQuantity
is a function updateCartQuantity
是 function
export const updateCartQuantity = (productId, quantity) => {
return {
type: 'UPDATE_CART_QUANTITY',
payload: {
productId,
quantity: quantity
}
}
};
it's data is handled here它的数据在这里处理
case 'UPDATE_CART_QUANTITY':
let item = cart.find(item => item.product.id === action.payload.productId);
let newCart = cart.filter(item => item.product.id !== action.payload.productId);
item.quantity = action.payload.quantity;
newCart.push(item);
return {
...state,
cart: newCart
};
problem is probably in this case
but I just can't see it问题可能是在这种
case
,但我只是看不到它
Okay.好的。 It is fixed thanks to @HMR comment on post.
由于@HMR 对帖子的评论,它已得到修复。 I just realised what I was doing.
我才意识到我在做什么。
Instead of cart.filter
I should use this而不是
cart.filter
我应该使用这个
let newCart = cart.map(c => c.id === action.payload.productId ? {...c, quantity: action.payload.quantity} : c);
and remove并删除
newCart.push(item);
.map will take care of looping through elements so there is no need of pushing element to newCart.push(item)
.map 将负责循环遍历元素,因此无需将元素推送到
newCart.push(item)
Thank you for your comment.感谢您的评论。 Hope this helps someone:)
希望这对某人有帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.