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

189
Views
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 answers
Answer question

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 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!