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

123
Visualizações
Reactjs functional compontent call api twice

I just created a simple component in ReactJs and call an api with axios but I noticed in browser network tab that my api called twice in first render, also I tested with a console log, it return twice too! why? I googled and people said this is normal in functional component and normal behavior but I don't want request to api twice! any idea?

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

export default function User() {
  useEffect(() => {
    getUser().then(); // it call api twice, I got data twice!
    console.log(111); // return twice
  }, []);

  const getUser = async () => {
    const user = await Api.Get("User");

    if (user.status === 200) {
      console.log(user);
    }
  };

  return (
    <>
      <div>Hello</div>
    </>
  );
}

Here I use user compontent: Router.js

<BrowserRouter basename="/">
<Routes>
     <Route exact path='/user' element={<User/>}></Route>
</Routes>
</BrowserRouter>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

React 18 introduces a new development-only check to Strict Mode. This check automatically unmounts and remounts the component, making the useEffect hook fire twice on the initial mount.

More there:

https://levelup.gitconnected.com/react-18-the-trickiness-of-useeffect-fadfa65fa4b4

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