Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda