Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!