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

201
Views
Cómo verificar si el artículo ya existe en su carrito de compras - UseReducer React JS

Tuve este problema donde necesitaba hacerlo para que un artículo no se pueda agregar dos veces en mi carrito de compras, luego busqué en esta plataforma para ver si había alguna respuesta. Descubrí que se hizo la pregunta Verifique si el artículo ya existe en el carrito al agregar REACT pero no hubo suficientes contribuciones en él. Finalmente, resolví el problema por mi cuenta, pero no se me permitió contribuir en los comentarios (soy nuevo aquí). Así que decidí dejar esto aquí en caso de que alguien necesite lo mismo.

Uso la API de contexto y useReducer. Aquí está mi código

 switch (action.type) { case ADD_T0_CART: return { ...state, basket: [...state.basket, action.payload] } ``` After some hours of headache i eventually fixed it using a simple JS find() function. ``` export const commerceReducer = (state,action) => { switch (action.type) { case ADD_T0_CART: const itemExists = state.basket.find( (item) => item.id === action.payload.id ) console.log(itemExists) if (!itemExists) return { ...state, basket: [...state.basket, action.payload] } ``` Hope someone finds this helpful.
about 4 years ago · Juan Pablo Isaza
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!