【发布时间】:2021-08-15 09:51:38
【问题描述】:
代码似乎很好,但它给出了这个错误。它说:TypeError:无法在 React Project 中读取 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
【问题讨论】:
标签: javascript reactjs typeerror