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

262
Views
Error: las cadenas de ID de los productos se guardaron como una matriz

Estoy tratando de almacenar la identificación de cada producto en los elementos del carrito, sin embargo, la identificación se guarda como una matriz, como se muestra en la imagen a continuación. En lugar de guardarse como esta id: "aRLMZkiSU7T0lcsPCSsV"

¿Cómo puedo solucionar esto?

ingrese la descripción de la imagen aquí

zona de pruebas de código: https://codesandbox.io/s/add-to-cart-jsd9fd?file=/src/App.js:0-2587

A continuación se muestran los códigos:

 export default function App() { const [cartItems, setCartItems] = useState([]); const handleAdd = (id, name, size, cat, color) => { console.log("add", id, name, size, cat, color); const productExist = cartItems.find((item) => item.id === id); if (productExist) { setCartItems( cartItems.map((item) => item.id === id ? { ...productExist, quantity: productExist.quantity + 1 } : item ) ); } else { setCartItems([ ...cartItems, { ...id, name, size, cat, color, quantity: 1 } ]); } }; // const handleAdd = (item) => { // console.log("add", item); // }; console.log(cartItems); return ( <div className="App"> {products.map((index) => ( <ul key={index.id}> <li> <b>{index.prodName}</b> </li> <li>Category: {index.cat}</li> <li>Size: {index.size}</li> {Object.entries(index.colorMap).map((color) => ( <> {color[1] !== 0 ? ( <> {color[0]} <button onClick={(e) => handleAdd( index.id, index.prodName, index.size, index.cat, color[0] ) } > Add to Cart </button> {/* <button onClick={(e) => handleAdd(index)}>Add to Cart</button> */} </> ) : ( <>2</> )} <br /> </> ))} Php {index.price}.00 </ul> ))} {/* <Cart cartItems={cartItems} /> */} </div> ); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Elimine el operador de propagación de Id.

 else { setCartItems([ ...cartItems, { id, name, size, cat, color, quantity: 1 } ]); }
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!