【问题标题】:How Can I Show An Empty Cart?如何显示空购物车?
【发布时间】:2020-11-01 00:47:00
【问题描述】:

我一直在为购物车编写一些(相当草率的)代码。但我有一个问题,每当购物车为空时,页面将无法加载。我这里有代码。我能做些什么来解决这个问题?我正在考虑制作一个 EmptyCart 常量并在没有数据时激活它?但我不确定。这是代码。谢谢

import React, { useEffect, useState } from 'react'
import { useDispatch } from 'react-redux';
import {
    removeCartItem,
    onSuccessBuy
} from '../../../_actions/user_actions';
import UserCardBlock from './Sections/UserCardBlock';
import { Result, Empty, Button } from 'antd';
import Paypal from '../../utils/Paypal';



function CartPage(props) {
    const dispatch = useDispatch();
    console.log(props)
    const [Total, setTotal] = useState(props.location.state.data.price)
    const [ShowTotal, setShowTotal] = useState(true)
    const [ShowSuccess, setShowSuccess] = useState(false)
    
    const removeFromCart = (productId) => {
        dispatch(removeCartItem(productId))
            }
        
    const transactionSuccess = (data) => {
        dispatch(onSuccessBuy({
            cartDetail: props.user.cartDetail,
            paymentData: data
        }))
            .then(response => {
                
                    setShowSuccess(true)
                    setShowTotal(false)
                }
            )
    }

    const transactionError = () => {
        console.log('Paypal error')
    }

    const transactionCanceled = () => {
        console.log('Transaction canceled')
    }
    const propductList = (data) =>{
      console.log(data)
      setTotal(data)
    }
    return (
        <div style={{ width: '85%', margin: '3rem auto' }}>
            <h1>My Cart</h1>
            <div>
                <UserCardBlock
                    productData={props.location.state.data}
                    removeItem={removeFromCart}
                    productList = {data=>propductList(data)}
                >

                </UserCardBlock>
 {ShowTotal ? (
          <div style={{ marginTop: "3rem" }}>
            <h2>Total amount: ${Total * 15} </h2>
          </div>
        ) : ShowSuccess ? (
          <Result status="success" title="Successfully Purchased Items" />
        ) : (
          <div
            style={{
              width: "100%",
              display: "flex",
              flexDirection: "column",
              justifyContent: "center",
            }}
          >
            <br />
            <Empty description={false} />
            <p>No Items In The Cart</p>
          </div>
        )}
      </div>

            {/* Paypal Button */}

            {ShowTotal &&
                <Paypal
                    toPay={Total}
                    onSuccess={transactionSuccess}
                    transactionError={transactionError}
                    transactionCanceled={transactionCanceled}
                />
            }
        </div>
    )
}

export default CartPage

【问题讨论】:

    标签: javascript node.js reactjs shopping-cart


    【解决方案1】:

    在尝试渲染 UserCardBlock 组件之前,您可以尝试添加三元检查以查看是否有道具数据。

    {props.location.state.data &&
            <UserCardBlock
              productData={props.location.state.data}
              removeItem={removeFromCart}
              productList={data => propductList(data)}
            />
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      相关资源
      最近更新 更多