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

90
Views
Reactjs useEffect no cambia el valor de estado al obtener datos

Estoy tratando de implementar una ruta protegida, que primero intenta obtener una autenticación (llamada api) para que pueda mostrar la ruta. Pero de alguna manera el valor del estado no cambia... ¿Tienes alguna idea?

 const ProtectedRoute = ({ component: Component, ...rest }) => { const [isAuthenticated, setIsAuthenticated] = useState(false); const fetch = async () => { const result = await axios.get("http://localhost:5000/auth/", { withCredentials: true, }); if (result.status >= 200 && result.status < 300) { setIsAuthenticated(true); } else { setIsAuthenticated(false); } }; useEffect(() => { fetch(); }, []); return ( <Route {...rest} render={(props) => { if (isAuthenticated) { return <Component {...props} />; } else { return <Redirect to={"./loginUser"} />; } }} /> ); }; export default ProtectedRoute;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo que puede hacer es devolver algo cuando la llamada a la API aún se está cargando. Algo como esto :

 const ProtectedRoute = ({ component: Component, ...rest }) => { const [isAuthenticated, setIsAuthenticated] = useState(undefined); const [isLoading, setIsLoading] = useState(false); const fetch = async () => { const result = await axios.get("http://localhost:5000/auth/", { withCredentials: true, }); if (result.status >= 200 && result.status < 300) { setIsAuthenticated(true); } else { setIsAuthenticated(false); } }; useEffect(() => { setIsLoading(true); fetch(); setIsLoading(false); }, []); return ( <Route {...rest} render={(props) => { if(isLoading) { // do something } else if (isAuthenticated !== undefined && !isLoading) { return <Component {...props} />; } else { return <Redirect to={"./loginUser"} />; } }} /> ); }; export default ProtectedRoute;
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!