首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Hook useState未更新UI

React Hook useState未更新UI
EN

Stack Overflow用户
提问于 2020-01-11 09:36:21
回答 2查看 4.5K关注 0票数 2

我是React Hooks的新手,我正在尝试使用以下代码更新购物车中的数量;

代码语言:javascript
复制
import React, { useState, useEffect } from "react";
import cookie from "react-cookies";
import CheckoutItems from "./CheckoutItems";
import restHelper from "../../shared/RestHelper";

const Checkout = () => {
    const [cart, setCart] = useState(null);

    useEffect(() => {
        const cartId = cookie.load("RbCartId");

        if (cartId){
            (async () => {
                const cart = await restHelper.getUserCart(cartId);
                setCart(cart);
            })();
            }
    }, []);

    const handleQtyUpdate = (evt, id) => {
        let _cart = cart;
        let items = _cart.cartItems.filter(x => x.id === id);
        let cartItem = { ...items[0] };
        cartItem.quantity = Number(evt.target.value);
        const index = _cart.cartItems.findIndex(x => x.id === cartItem.id);
        _cart.cartItems[index] = cartItem;
        setCart(_cart);
    };

    return (
        <section>
            <div className="container" style={{ marginTop: "80px" }}>
                <h3 className="rb-highlight">Shopping Cart</h3>
                <table className="table" style={{marginTop: "20px"}}>
                    <thead>
                        <tr>
                            <th>Items</th>
                            <th style={{ textAlign: "right" }}>Price</th>
                            <th style={{ textAlign: "right" }}>Quantity</th>
                            <th style={{ textAlign: "right" }}>Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <CheckoutItems cart={cart} onQtyUpdate={handleQtyUpdate} />
                    </tbody>
                </table>
            </div>
        </section>
    );
}
export default Checkout;

CheckoutItems组件是

代码语言:javascript
复制
import React from "react";

function CheckoutItems({cart, onQtyUpdate}){
    if (!cart || cart.cartItems === 0){
        return <tr><td colSpan="4"><span className="rb-highlight">There are no items in your cart</span></td></tr>;
    }
    else{
        const cartItems = cart.cartItems.map((item, index) => {
            return <tr key={index}>
                <td>{item.description}</td>
                <td style={{textAlign: "right"}}>&euro;{item.cost}</td>
                <td style={{textAlign: "right"}}><input type="text" id={item.id} name="quantity" value={item.quantity} onChange={(evt) => onQtyUpdate(evt, item.id)} /></td>
                <td style={{textAlign: "right"}}>&euro;{item.cost * item.quantity}</td>
            </tr>
        })
        return cartItems;
    }
}

export default CheckoutItems;

购物车项目在handleQtyUpdate中成功更新,但是值不会在UI中更新。根据我所读到的,我应该使用useEffect来更新购物车,但我不确定如何在handleQtyUpdate中使用它。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-11 09:50:07

问题是状态比较很浅,这意味着,在对象的情况下,只会比较引用。

在您的示例中,您更新了cartItems属性,但没有更新对象实例,因此就React而言,状态没有改变,因为它不会检查单个属性。

尝试每次创建一个全新的购物车,您应该会看到UI更新,例如

代码语言:javascript
复制
 setCart({ ..._cart })
票数 18
EN

Stack Overflow用户

发布于 2020-01-11 09:52:31

你应该有一个使用扩展操作符的对象的副本

setCart({..._cart})

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59690934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档