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

110
Visualizações
React -hook UseEffect memory Leak issue more than one function call

Hello Everyone I am new to react-hook I have some doubts about the useEffect call method

1 . I have one warning issue react Hook useEffect has a missing dependency "get data". Either include it or remove the dependency array react-hooks/exhaustive-deps I fixed that issue by using this command line // eslint-disable-next-line react-hooks/exhaustive-deps, if I am doing like this correct or not if any issue occurs in future production server.

  1. I have another issue in useEffect if I routing quickly I got this kind of error "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" I tried to search fix the problem I saw the answer in a single API call in the within inside useEffect, I have a more than one get API call how can I write a cleanup function, Any Ideas or Links

Thanks for Help

const [user,setUser]=useState([]);
const [post,setPost]=useState([]);

const UserList =()=>{
  axios.get("https://jsonplaceholder.typicode.com/users")
.then((res)=>{
  setUser(res.data)
 })
}

const PostList =()=>{
  axios.get("https://jsonplaceholder.typicode.com/post")
.then((res)=>{
  setPost(res.data)
 })
}

useEffect (()=>{
PostList();
UserList();
},[])
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your API calls are not asynchronous, so the state is not updated until the API call is complete, but the components are rendered onto the screen which depends on the values of the state. Make your API calls async.

about 4 years ago · Juan Pablo Isaza Relatório

0

Memory leaks happens when you try to set a state after your component is unmounted and this often happens when you set your state in async/promise functions.

One solution that I have been using for a while is initiating a variable outside component and check that variable before updating your state.

let isMounted = true;
const YourComponent = () => {
  const UserList = () => {
    axios.get("https://jsonplaceholder.typicode.com/users")
    .then((res)=>{
      if(isMounted){
        setUser(res.data)
      }
    })
  }

  useEffect(() => {
    UserList();
    return () => {
      isMounted = false;
    }
  }, []}
}
about 4 years ago · Juan Pablo Isaza Relatório

0

after fetch data, need cleanup function. UseEffect(callback, deps) allows you to easily cleanup side-effects. When the callback function returns a function, React will use that as a cleanup function

useEffect(() => {
let controller = new AbortController();    (async () => {
  try {
   //fetch
  } catch (e) { 
    // Handle fetch error
  }
})();
return () => controller?.abort();  }, []);

obviously this is one option how you can deal with it

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