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

155
Views
ReactJS Warning: Maximum update depth exceeded when setting a state inside useEffect

I'm trying to create the pagination logic with filtering but When I try to change the state setTotalProducts at useEffect it throws the warning of maximum update depth exceeded at any case totalProducts is in the deps array.

const [totalProducts, setTotalProducts] = useState(products); // products is a previously fetched data
  const [filteredProducts, setFilteredProducts] = useState(totalProducts);
 
  const productsPerPage = 9;
  const indexOfLastProduct = currentPage * productsPerPage;
  const indexOfFirstProduct = indexOfLastProduct - productsPerPage;

  useEffect(() => {
    setFilteredProducts(
      totalProducts.slice(indexOfFirstProduct, indexOfLastProduct)
    );

    // Sidebar filters
    if (Object.entries(filters).length > 0) {
      const arr = filterSidebar(products, filters); // returns an array of filtered data
      setTotalProducts(arr); // Setting this state causes the problem
    }
   }, [filters, currentPage, totalProducts]); // It won't work as expected in case of removing totalProducts
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!