反应:无法根据目标正确更新数组

如果有人可以帮助我,我将不胜感激。

目标:如果通过单击“+”添加的项目:

  1. 它需要检查订单是否已经存在(如果存在,则增加数量)

  2. 如果没有,则将该项目附加到订单列表中

fullmenu包含以下架构中所有项目的列表:

{  _id: 5eef61450bd95e1f5c8f372f ,

  name: "Burger"   ,

  category: "American"  ,

  price: "100"  ,

  isVeg: false,

  qty: 0

}

ISSUE:即使它已经存在于订单数组中,它也会添加一个新的 item 对象,而不是仅仅增加它的数量


主要代码逻辑:


    const [total, setTotal] = useState(0);

    const [orders, setOrders] = useState([]);

    const [fullMenu, setFullMenu] = useState();

    const addMore = (e) => {

        let sign = e.target.innerText;

        let id = e.target.id;

        const newOrders = [...orders];

        let order = newOrders.find(each => each.id === id);

        // if the order already exists for the item

        if (order) {

            let _sign = sign === "+" ? 1 : -1;

            // increase or decrease the quantity

            order.qty = (order.qty || 0) + 1 * _sign;

            // remove item from order list if qty = 0

            if(!order.qty){

                setOrders(orders => orders.filter(each => each.id !== id));

                // setTotal(total => total - parseInt(order.price));

                return;

            };

            // save 

            setOrders(newOrders);

            if (sign === "+") setTotal(total => total + parseInt(order.price));

            if (sign === "-") setTotal(total => total - parseInt(order.price));

            return;

        };

        // if item doesn't exist in the order list

        const item = fullMenu.find(each => each._id === id);

        if (sign === "+") {

            order = {

                ...item, qty: 1 

             };

            setOrders(orders => [...orders, order]);

            setTotal(total => total + parseInt(order.price));

        };

    }

JSX:


<div className="col-sm-4"><span id={each._id} onClick={addMore} category={each.category}>-</span></div>

<div className="col-sm-4"><FormCheckLabel className="qty" >{each.qty}</FormCheckLabel></div>

<div className="col-sm-4"><span id={each._id} onClick={addMore} category={each.category}>+</span></div>


繁星淼淼
浏览 112回答 3
3回答

斯蒂芬大帝

您需要使用each._idnot查找订单each.id...let order = newOrders.find(each => each._id === id);...编辑: 注意到的另一件事是你直接改变了状态重构代码const addMore = (e) => {&nbsp; let sign = e.target.innerText;&nbsp; let id = e.target.id;&nbsp; const newOrders = [...orders];&nbsp; let orderIndex = newOrders.findIndex((each) => each._id === id); //<---use correct ._id&nbsp; if (orderIndex !== -1) {&nbsp; &nbsp; let _sign = sign === "+" ? 1 : -1;&nbsp; &nbsp; // order.qty = (order.qty || 0) + 1 * _sign; //<----don't mutate like this&nbsp; &nbsp; const updatedOrder = {...newOrders[orderIndex]};&nbsp; &nbsp; updatedOrder.qty = (updatedOrder.qty || 0) + 1 * _sign;; //<---update state like this&nbsp; &nbsp; if (!updatedOrder.qty) {&nbsp; &nbsp; &nbsp; setOrders((orders) => orders.filter((each) => each._id !== id)); //<---use correct ._id&nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; // save&nbsp; &nbsp; newOrders[orderIndex] = updatedOrder;//<----save updated order like this&nbsp; &nbsp; setOrders(newOrders);&nbsp; &nbsp; if (sign === "+") setTotal((total) => total + parseInt(order.price));&nbsp; &nbsp; if (sign === "-") setTotal((total) => total - parseInt(order.price));&nbsp; &nbsp; return;&nbsp; }&nbsp; const item = fullMenu.find((each) => each._id === id);&nbsp; if (sign === "+") {&nbsp; &nbsp; order = {&nbsp; &nbsp; &nbsp; ...item,&nbsp; &nbsp; &nbsp; qty: 1,&nbsp; &nbsp; };&nbsp; &nbsp; setOrders((orders) => [...orders, order]);&nbsp; &nbsp; setTotal((total) => total + parseInt(order.price));&nbsp; }};

杨__羊羊

let order = newOrders.find(each => each.id === id);应该each._id你忘了else:const [total, setTotal] = useState(0);&nbsp; &nbsp; const [orders, setOrders] = useState([]);&nbsp; &nbsp; const [fullMenu, setFullMenu] = useState();&nbsp; &nbsp; const addMore = (e) => {&nbsp; &nbsp; &nbsp; &nbsp; let sign = e.target.innerText;&nbsp; &nbsp; &nbsp; &nbsp; let id = e.target.id;&nbsp; &nbsp; &nbsp; &nbsp; const newOrders = [...orders];&nbsp; &nbsp; &nbsp; &nbsp; let order = newOrders.find(each => each.id === id);&nbsp; &nbsp; &nbsp; &nbsp; // if the order already exists for the item&nbsp; &nbsp; &nbsp; &nbsp; if (order) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let _sign = sign === "+" ? 1 : -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // increase or decrease the quantity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.qty = (order.qty || 0) + 1 * _sign;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // remove item from order list if qty = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!order.qty){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setOrders(orders => orders.filter(each => each.id !== id));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // setTotal(total => total - parseInt(order.price));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // save&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setOrders(newOrders);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sign === "+") setTotal(total => total + parseInt(order.price));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sign === "-") setTotal(total => total - parseInt(order.price));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Should be in else block like this&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if item doesn't exist in the order list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const item = fullMenu.find(each => each._id === id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sign === "+") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...item, qty: 1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setOrders(orders => [...orders, order]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTotal(total => total + parseInt(order.price));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

沧海一幻觉

这终于奏效了。进行了小调整:const [total, setTotal] = useState(0);&nbsp; &nbsp; const [orders, setOrders] = useState([]);&nbsp; &nbsp; const [fullMenu, setFullMenu] = useState();&nbsp; &nbsp; const addMore = (e) => {&nbsp; &nbsp; &nbsp; &nbsp; let sign = e.target.innerText;&nbsp; &nbsp; &nbsp; &nbsp; let id = e.target.id;&nbsp; &nbsp; &nbsp; &nbsp; const newOrders = [...orders];&nbsp; &nbsp; &nbsp; &nbsp; let orderIndex = newOrders.findIndex(each => each._id === id);&nbsp; &nbsp; &nbsp; &nbsp; // if the order already exists for the item&nbsp; &nbsp; &nbsp; &nbsp; if (orderIndex !== -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let _sign = sign === "+" ? 1 : -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // increase or decrease the quantity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const updatedOrder = {...newOrders[orderIndex]};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updatedOrder.qty = (updatedOrder.qty || 0) + 1 * _sign;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // remove item from order list if qty = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!updatedOrder.qty){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setOrders(orders => orders.filter(each => each._id !== id));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTotal(total => total - parseInt(updatedOrder.price));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // save&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newOrders[orderIndex] = updatedOrder;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setOrders(newOrders);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sign === "+") setTotal(total => total + parseInt(updatedOrder.price));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sign === "-") setTotal(total => total - parseInt(updatedOrder.price));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; // if item doesn't exist in the order list&nbsp; &nbsp; &nbsp; &nbsp; const item = fullMenu.find(each => each._id === id);&nbsp; &nbsp; &nbsp; &nbsp; if (sign === "+") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let order = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...item, qty: 1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setOrders(orders => [...orders, order]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTotal(total => total + parseInt(order.price));&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript