React:setState 不会重新渲染组件

joa*_*otg 2 javascript reactjs

我正在为电子商务网站实施购物车。购物车是一个由对象数组表示的状态变量shopCart。每个对象都包含有关产品的信息,例如标题和价格。我正在尝试实现一个删除按钮,它实际上正在执行它的意图,即从shopCart状态中删除项目,但更改未显示在屏幕渲染上。我可以清空购物车,但屏幕仍然显示产品。这是购物车页面的主要代码:

return (
            <div class={styles.container}>
                <h1>Product</h1><h1>Quantity</h1><h1>Unit price</h1><h1>Total price</h1><div></div>
                {
                    shopCart.map((product, i, array)   => <CartItem key={product.id} product={product} index={i} array={array}/>)
                }
            </div>
        )
Run Code Online (Sandbox Code Playgroud)

这是 CartItem.js 的实现

const CartItem = (props) => {
    let { shopCart, setShopCart } = useContext(Context);
    let product = props.product;

// takes the identification of a shopping cart product and removes it from the cart
    const decrease = (element) => {
        shopCart.forEach((el, i) => {
            if (el.hasOwnProperty('id')) {
                if (el.id === element) {
                    let aux = shopCart;
                    aux.splice(i, 1);
                    setShopCart(aux);
                }
            }
        })
    }

    return (
        <div>
            <img src={product.image}></img>
            <h1>{product.quantity}</h1>
            <h1>{product.price}</h1>
            <h1>{product.price * product.quantity}</h1>
            <button onClick={() => {
                decrease(product.id);
            }}>Remove</button>
        </div>
    )
}

Run Code Online (Sandbox Code Playgroud)

为什么它不能正确呈现购物车,即使每次点击删除按钮后都会删除购物车项目?

Dre*_*ese 5

问题

你正在改变状态。您保存对 state 的引用,对其进行变异,然后将其保存回 state,因此数组引用永远不会改变。React 在检查 state 或 props 是否更新时使用浅等式。

const decrease = (element) => {
    shopCart.forEach((el, i) => {
        if (el.hasOwnProperty('id')) {
            if (el.id === element) {
                let aux = shopCart; // <-- Saved state ref
                aux.splice(i, 1);   // <-- mutation
                setShopCart(aux);   // <-- Saved ref back to state
            }
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

解决方案

在反应状态下更新数组的正确方法是将数组元素复制到新的数组引用中。这可以通过按项目 id 过滤当前购物车来轻松实现。我还建议更改参数名称,以便更清楚它代表什么。

const decrease = (id) => {
  setShopCart(shopCart => shopCart.filter(item => item.id !== id));
}
Run Code Online (Sandbox Code Playgroud)