简体   繁体   English

当我在 REACT 中更新输入值时,为什么我的项目会更改顺序?

[英]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.

相关问题 为什么即使使用 onChange 监听器我也不能在 React 中更改我的输入值 - Why can't I change my input value in React even with the onChange listener 当我更新 state 时,我的输入没有更新值 - My input didn't update value when I update state 如何更新具有数组的useState,在该数组中我有对象,当输入值在react js中发生变化时,该对象将更新 - How to update a useState which has an array, inside this array I have objects this objects will update when the input value will change in react js 为什么更改模型时输入不更新? - Why the input does not update when i change the model? React js,为什么在更改open的值时我的组件不重新渲染? - React js, why doesn't my component re-render when I change the value of open? JavaScript - 为什么当“Document.querySelector().value”在 function 之外时我的代码不运行 - JavaScript - Why doesn't my code run when "Document.querySelector().value" is outside function + how do I get function to run on input number change? 如何从 class 组件更新功能组件的 state 以便在反应中显示 map 中的项目 - How do I update state of a functional component from a class component in order to display items from a map in react 为什么不能使用输入更改值? - Why can't I change the value using my input? 在输入值更改时更新其他组件 React - Update other component on input value change React 当我设置初始值时,React Input 不会改变 - React Input don't change when I set a initial value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM