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

135
Visualizações
Can't perform a React state update on an unmounted component after API fetch

I am writing a React component where I fetch data from an API and then I refresh the component with states, but for some reasons I always get this error:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. at CollectionContainer (http://localhost:3000/static/js/bundle.js:1632:102)

I tried solutions to similar questions but nothing worked, please bear with me, I'm new to React

Code

import React from "react";
import { useState } from "react";

function CollectionContainer(props) {
  //STATE
  const [FilesAPIfetched, setFilesAPIfetched] = useState(false);

  function RefreshDataDatasets() {
    console.log("Refreshed collections data");
    setFilesAPIfetched(true);
  }

  const files = ["test"];
  const COLLECTION_ID = props.children.id;

  //API CALL
  let API_CALL_FILES = `http://xxx.yyy/${COLLECTION_ID}/files/`;
  fetch(API_CALL_FILES)
    .then((res) => res.json())
    //.then((data) => console.log(data));
    .then((data) => {
      for (let i = 0; i < data.length; i++) {
        files[i] = data[i];
      }
      console.log(files);

      RefreshDataDatasets();
    });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the Effect Hook to call the API request after the page renderization. Effect Hook is similar to ComponentDidMount and componentDidUpdate when you build Class Components in React, but when you use Functional Components, you need to use Effect Hook.

const getApiData = () => {
  // API CALL
}

useEffect(() => {
  getApiData();
});
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