Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Cannot read properties of undefined (length) when passing props

I am trying to make a shopping app where if the cart is empty, it displays a div that says "shopping cart is empty." But I am getting a type error that says "Cannot read properties of undefined (reading length)." I am passing down props to my Cart.js and am really stuck. Any suggestions?

import React from 'react';
// import 'semantic-ui-css/semantic.min.css'
import { Routes, Route } from 'react-router-dom'
import { useState } from 'react';

import Nav from './components/Nav';
import NavTwo from './components/NavTwo';
import HotSandwiches from './pages/HotSandwiches';
import Home from './pages/Home';
import ColdSandwiches from './pages/ColdSandwiches';
import ChickenMeals from './pages/ChickenMeals';
import Chocolate from './pages/Chocolate';
import Drinks from './pages/Drinks';
import FruitSnacks from './pages/FruitSnacks';
import SaltySnacks from './pages/SaltySnacks';
import GumandMints from './pages/GumandMints';
import Toiletries from './pages/Toiletries';
import Cart from './components/Cart';

const App = () => {
  const [cartItems, setCartItems] = useState([])

  return (
    <div>
      <Nav />
      <NavTwo />
      <Routes>
        <Route path='/' element={<Home />} />
        <Route path='hotsandwiches' element={<HotSandwiches />} />
        <Route path='coldsandwiches' element={<ColdSandwiches />} />
        <Route path='chickenmeals' element={<ChickenMeals />} />
        <Route path='chocolate' element={<Chocolate />} />
        <Route path='drinks' element={<Drinks />} />
        <Route path='fruitsnacks' element={<FruitSnacks />} />
        <Route path='saltysnacks' element={<SaltySnacks />} />
        <Route path='gumandmints' element={<GumandMints />} />
        <Route path='toiletries' element={<Toiletries />} />
        <Route path='cart' element={<Cart />} cartItems={cartItems} />
      </Routes>
    </div>
  );
}

export default App;

import React from 'react';

const Cart = ( {cartItems} ) => {
  return (
    <div>
      Cart items
      <div>
        {cart Items . length() === 0 ? <div> No items in cart </div> : <div> Hello </div>}
      </div>
    </div>
  );
} 

export default Cart;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your main routes, you're declaring:

<Route path='cart' element={<Cart />} cartItems={cartItems} />

The <Route> component doesn't pass its unused props to its children. You can specify it inside element:

<Route path='cart' element={<Cart cartItems={cartItems} />} />

Also, not sure if this is a typo, but in your cart file it should be cartItems.length instead of cart Items.length() (there's no space in between and no () (function call; it's just an object property)).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!