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

275
Visualizações
Many http requests are being made when I visit a React route even though only one request is being called

Whenever I visit the "/" route and I go to the network section in inspect, I see a bunch of requests going to "/api/checkauth". I only call the request "/api/checkauth" once, when I visit the "/" route. Maybe its something to do with element={...}. I don't know how to fix this issue.

This what I see in the network section when I visit it once

import './App.css';
import { React, useState } from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

import Login from "./components/Login";
import Signup from "./components/Signup";
import Feed from "./components/User/Feed";
import UserProfile from "./components/User/UserProfile";

const axios = require("axios");

function App() {
  const [isAuth, setAuth] = useState(false);

  const checkAuthHome = () => { 
    axios.post("http://localhost:5000/api/checkauth", {}, { withCredentials: true })
    .then((response) => {
      setAuth(response.data.status);;
    });
    if (isAuth === true) {
      return (<Feed />);
    } else {
      return (<Login />);
    }
  }

  return (
    <div className="App">
      <Router>
        <Routes>
          <Route exact path="/" element={checkAuthHome()}/>
          <Route exact path="/signup" element={<Signup />}/>
          <Route exact path="/users/:id" element={<UserProfile />}/>
        </Routes>
      </Router>
    </div>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You must not do data fetching in component render. Try to use useEffect:

function App() {
  const [isAuth, setAuth] = useState(false);

  useEffect(()=>{
    axios.post("http://localhost:5000/api/checkauth", {}, { withCredentials: true })
      .then((response) => {
        setAuth(response.data.status);;
      });
  }, [])

  const checkAuthHome = () => { 
    if (isAuth === true) {
      return (<Feed />);
    } else {
      return (<Login />);
    }
  }

  return (
    <div className="App">
      <Router>
        <Routes>
          <Route exact path="/" element={checkAuthHome()}/>
          <Route exact path="/signup" element={<Signup />}/>
          <Route exact path="/users/:id" element={<UserProfile />}/>
        </Routes>
      </Router>
    </div>
  );
}
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