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

222
Visualizações
"Fethcing and loading" function causes "Can't perform a React state update on an unmounted component" error

I am quite new in react native and found simple solution from tutorial for fetching&loading case

export const useFetching = (callback) => {
    const [isLoading, setIsLoading] = useState(false);
    const [error, setError] = useState('');

    const fetching = async(data) => {
        try{
            setIsLoading(true);
            await callback(data);
        } catch(e) {
            setError(e.message);
        } finally {
            setIsLoading(false);
        }
    }
    return [fetching, isLoading, error];
}

I use it with signUp func from react-navigation docs as a callback, which is supposed to change the screen to main app:

signIn: async (data) => {
    const token = await AuthManager.login(data);
    if (token)
        dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' });

But when I call it I get "Can't perform a React state update on an unmounted component". I found out that:

  1. The "finally" block from useFetching causes the error
  2. Empty useEffect useEffect(() => {return () => {}},[]) inserted into login screen removes the problem

I'm wondering, is there any normal solution in this case and why useEffect helps here?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Changing state on unmounted component is illegal measure in react.

This is simple events plan in your component life cycle:

  1. Api request sended
  2. Component was unmounted
  3. Api request returns with data to your app
  4. Ooooops, api caller (in your case it is fetch function) see that component was unmounted and return error, so your app execute this block of code: catch(e) { setError(e.message); }, but there is problem - setState (setError).

finally block code executed too, so you must check is component mounted before setState.

Simple solution

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