Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
TypeError: state.find is not a function

I try to use the find() method but I get

cartItems.find is not a function

what happens? and how to fix it?

import React, {useState} from 'react'

function cart() {
    const [cartItems, setCartItems] = useState([])

    const add = (pro) => {
        const exist = cartItems.find((item) => {
            return item.id === pro.id;
        });
        if (exist) {
            setCartItems(
                cartItems.find((item) => {
                    return item.id === exist.id
                        ? {...exist, qnty: exist.qnty + 1}
                        : item;
                })
            );
        } else {
            setCartItems([...cartItems, {...pro, qnty: 1}]);
        }
    };
    return (
        <div>cart</div>
    )
}

export default cart
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This happen because cartItems is no longer an array.

According to here:

The find() method returns the first element in the provided array that satisfies the provided testing function.

In this part of your code -

setCartItems(
  cartItems.find((item) => {
    return item.id === exist.id
      ? {...exist, qnty: exist.qnty + 1}
      : item;
    })
);

You set cartItems to be the first element of your condition there with find(). So next render, cartItems is no longer an array, but a an item. Which have no find.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda