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

187
Views
Filtering Table data with useEffect Hook causing component to re-render infinitely

I'm trying to use a search bar component to dynamically filter the content of a table that's being populated by API requests, however when I use this implementation the component re-renders infinitely and repeatedly sends the same API requests. The useEffect() Hook:

React.useEffect(() => {
        const filteredRows = rows.filter((row) => {
            return row.name.toLowerCase().includes(search.toLowerCase());
        });
        if (filteredRows !== rows){
            setRows(filteredRows);
        }        
    }, [rows, search]);

Is there something I've missed in this implementation that would cause this to re-render infinitely?

Edit 1: For further context, adding in relevant segments of code from that reference this component which might cause the same behaviour.

Function inside the parent component that renders the table which calls my API through a webHelpers library I wrote to ease API request use.

function fetchUsers() {
        webHelpers.get('/api/workers', environment, "api", token, (data: any) => {
            if (data == undefined || data == null || data.status != undefined) {
                console.log('bad fetch call');
            }
            else {
                setLoaded(true);   
                setUsers(data);
                console.log(users);
            }
        });
    }

    fetchUsers();

Edit 2: Steps taken so far to attempt to fix this issue, edited the hook according to comments:

 React.useEffect(() => {
        setRows((oldRows) => oldRows.filter((row) => {
            return row.name.toLowerCase().includes(search.toLowerCase());
        }));    
    }, [search]);

Edit 3: Solution found, I've marked the answer by @Dharmik pointing out how Effect calls are managed as this caused me to investigate the parent components and find out what was causing the component to re-render repeatedly. As it turns out, there was a useEffect hook running repeatedly by a parent element which re-rendered the page and caused a loop of renders and API calls. My solution was to remove this hook and the sub-components continued rendering as they should without loops.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It is happening because you've added rows to useEffect dependency array and when someone enters something into search bar, The rows get filtered and rows are constantly updating.

And because of that useEffect is getting called again and again. Remove rows from the useEffect dependency array and it should work fine.

about 4 years ago · Juan Pablo Isaza Report

0

I would like to complement Dharmik answer. Dependencies should stay exhaustive (React team recomendation). I think a mistake is that filteredRows !== rows uses reference equality. But rows.filter(...) returns a new reference. So you can use some kind of deep equality check or in my opinion better somethink like:

React.useEffect(() => {
        setRows((oldRows) => oldRows.filter((row) => {
            return row.name.toLowerCase().includes(search.toLowerCase());
        }));
    }, [search]);
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!