【问题标题】:return function doesn't render in React返回函数不会在 React 中呈现
【发布时间】:2021-11-10 12:27:58
【问题描述】:

我正在尝试在 React 中实现一个购物车。我为我的购物车创建了一个上下文来处理购买的数量,它工作正常。但是,我在将购物车上下文中的数据显示到购物车页面时遇到了问题。这是我将购买的商品显示到购物车页面的代码。

export class Cart extends React.Component {

    constructor(props) {
        super(props);
    }

    render(){
        return (
            <>
                <div>
                    <h1>Cart</h1>
                </div>
                <CartContext.Consumer>
                    {
                        (cartContext) =>{
                            const {productsToPurchase} = cartContext
                            console.log(productsToPurchase)
                            if (productsToPurchase.length === 0){
                                return(<h1>Cart is empty</h1>)
                            }
                            else {
                                productsToPurchase.map(
                                    (product) =>{
                                        // console.log works fine
                                        console.log("Reached Else Statment")
                                        console.log(product.itemId)
                                        // return doesn't work
                                        return(
                                            <h1 key={product.itemId + "-cart"}> 8525552 </h1>
                                        )
                                })
                            }
                        }
                    }
                </CartContext.Consumer>
            </>
        )
    }

}

我也做过同样的事情,而且效果很好。我不知道为什么我的映射函数不显示我需要的数据。请查看 cmets 了解更多详情

【问题讨论】:

    标签: javascript reactjs react-context


    【解决方案1】:

    您错过了退货。您应该返回 productsToPurchase.map() 的结果

    <CartContext.Consumer>
                        {
                            (cartContext) =>{
                                const {productsToPurchase} = cartContext
                                console.log(productsToPurchase)
                                if (productsToPurchase.length === 0){
                                    return(<h1>Cart is empty</h1>)
                                }
                                else {
                                    return productsToPurchase.map(
                                        (product) =>{
                                            // console.log works fine
                                            console.log("Reached Else Statment")
                                            console.log(product.itemId)
                                            // return doesn't work
                                            return(
                                                <h1 key={product.itemId + "-cart"}> 8525552 </h1>
                                            )
                                    })
                                }
                            }
                        }
                    </CartContext.Consumer>
    

    【讨论】:

    • 感谢您对我的帮助
    • 但我之前没有这样做,而且效果很好。这是为什么呢?
    • 这是因为在else 语句中,您调用了map() 函数,然后您返回h1,其中包含每件商品的产品信息,但您没有在外面返回任何东西函数(接收cartContext 的函数,正如您在if 语句中所做的那样。因此,您不会在else 案例中呈现任何内容
    • 我明白了。是的,之前和我一起工作的那个没有 if-else 语句。非常感谢
    猜你喜欢
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    相关资源
    最近更新 更多