javascript - 类型错误:无法在 React 项目中读取 null 的属性 'length

代码似乎没问题,但它给出了这个错误。它说类型错误:无法在 React 项目中读取 null 的属性“长度”。我已经发布了下面的代码。我正在使用 React Js 来构建它。 请帮忙。

import React, {useEffect} from 'react'
import { Link } from 'react-router-dom'
import { useDispatch, useSelector } from 'react-redux'
import { Row, Col, ListGroup, Image, Form, Button, Card } from 'react-bootstrap'
import Message from '../components/Message'
import { addToCart } from '../actions/cartActions'

function CartScreen({ match, location, history }) {
    const productId = match.params.id
    const qty = location.search ? Number(location.search.split('=')[1]) : 1
    
    const dispatch = useDispatch()

    const cart = useSelector(state => state.cart)
    const { cartItems } = cart

    useEffect(() => {
        if (productId){
            dispatch(addToCart(productId, qty))
        }
    }, [dispatch, productId, qty])


    return (
        <Row>
            <Col md={8}>
                <h1>Shopping Cart</h1>
                {cartItems.length === 0 ? (
                    <Message variant='info'>
                        Your cart is empty <Link to='/'>Go Back</Link>
                    </Message>
                ) : (
                        <ListGroup variant='flush'>
                            
                        </ListGroup>
                    )}
            </Col>
        </Row>
    )
}

export default CartScreen 

最佳答案

如错误所述,这只是因为您的 cartItems 为 null

变量可以为 null 并在 1 秒后定义,但是当变量为 null 时会出现此错误,因此您永远不会看到没有 null 值的变量。

这是解决问题的三种方法。

1)

{cartItems?.length === 0 ? ( // add a ?. to check if variable is null
  <Message variant='info'>
    Your cart is empty <Link to='/'>Go Back</Link>
  </Message>
) : (
  <ListGroup variant='flush'>

  </ListGroup>
)}
{cartItems && cartItems.length === 0 ? ( // you check if the var is defined before check the length
  <Message variant='info'>
    Your cart is empty <Link to='/'>Go Back</Link>
  </Message>
) : (
  <ListGroup variant='flush'>

  </ListGroup>
)}
function CartScreen({ match, location, history }) {
    // ...

    // add a conditional render
    if (!cartItems) return <p>Loading...</p>

    return (
        <Row>
            <Col md={8}>
                <h1>Shopping Cart</h1>
                {cartItems.length === 0 ? (
                    <Message variant='info'>
                        Your cart is empty <Link to='/'>Go Back</Link>
                    </Message>
                ) : (
                        <ListGroup variant='flush'>
                            
                        </ListGroup>
                    )}
            </Col>
        </Row>
    )
}

关于javascript - 类型错误:无法在 React 项目中读取 null 的属性 'length',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67716998/

相关文章:

git - 如何在 git pull 之前预览更改

javascript - 如何使用对象从对象数组javascript中删除它?

javascript - 如何在reactjs中将csv文件数据转换为json对象?

awk - 从使用 sed 获得的部分中获取上面的 N 行

loops - 为什么内循环收集不返回结果?

javascript - 使 href 中带有哈希 (#) 的每个 anchor 标记都不可点击

awk - Unix 提取两行之间的行并将它们存储在各自的文件中

r - 从数据框中的前一列中减去每一列

python - Tensorflow Metal 插件已注册错误

kotlin - 如何获取数组 Kotlin 中特定值的大小