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

195
Views
TypeError no capturado: no se pueden leer las propiedades de undefined (leyendo 'id')

Me sale indefinido porque item de cartitems no está indefinido, ¿cómo puedo solucionarlo?

1.

 import React,{useState} from 'react' import {products} from './data' function app() { const [cartitems, setCartitems] = useState([]) const onAddToCart = (product)=>{ const exist = cartitems.find((item)=> { return product.id == item.id }) if(exist){ setCartitems(cartitems.map((item)=>{ item.id == product.id ? {...exist, qnty: exist.qnty + 1}: item })) } else{ setCartitems([...cartitems, {...product, qnty: 1}]) } } return ( <div> {products.map((product)=>( <div key={product.id}> <img src={product.image} style= {{width:"200px"}}/> <p>{product.name}</p> <button onClick={() => onAddToCart(product)}>Add To Cart</button> </div> ))} </div> ) } export default app

 export const products = [ { id: '1', name: 'MacBook', price: 1400, image: 'https://picsum.photos/id/180/2400/1600', }, { id: '2', name: 'Old Car', price: 2400, image: 'https://picsum.photos/id/111/4400/2656', }, { id: '3', name: 'W Shoes', price: 1000, image: 'https://picsum.photos/id/21/3008/2008', }, ]

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

esto puede solucionar su problema:

1- no devolviste nada del mapa

2- es mejor usar el tipo de función en estado establecido para elementos de carrito

 function app() { const [cartitems, setCartitems] = useState([]) const onAddToCart = (product)=>{ const exist = cartitems.find((item)=> { return product.id == item.id }) if(exist){ setCartitems(cartitems.map((item)=> item.id == product.id ? ({...exist, qnty: exist.qnty + 1}): item )) } else{ setCartitems(s=>[...s, {...product, qnty: 1}]) } }
about 4 years ago · Juan Pablo Isaza Report

0

Problema

En su renderizado inicial, está intentando acceder a una propiedad que no existe. en otras palabras, en su representación inicial, sus products son una matriz vacía

Solución

Usar circuitos cortos

 {products && products.map((product) => ( <div key={product.id}> <img src={product.image} style={{ width: "200px" }} /> <p>{product.name}</p> <button onClick={() => onAddToCart(product)}> Add To Cart </button> </div> ))}
about 4 years ago · Juan Pablo Isaza Report

0

El problema es: no devuelves nada aquí en .map . Es por eso que te estás volviendo undefined .

 setCartitems(cartitems.map((item)=>{ item.id == product.id ? {...exist, qnty: exist.qnty + 1}: item }))

Simplemente elimine { y } :

 setCartitems(cartitems.map((item)=> item.id == product.id ? {...exist, qnty: exist.qnty + 1}: item ))

O agregue return explícitamente:

 cartitems.map(item => { if (item.id == product.id) { return { ...exist, qnty: exist.qnty + 1 } } return item })
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!