Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
React useEffect execute initial setup

I have a parent component that always needs to retrieve a token first before the subcomponents get executed. As discussed in this post What is the correct order of execution of useEffect in React parent and child components? the subcomponents useEffect gets executed first.

My question is how can I handle retrieving the token on initial loading (in the main component)?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I solved it by using a state to track if the session has been initialized. If the session has not been initialized its displaying a loading animation. This is only executed on the initial loading of the app. The loading component can't have a useEffect that requires the api with a valid token or user information.

function App() {
  const [hasSession, setSession] = useState(false)

  useEffect(() => {
    async function initUserProfile() {
      await apiService.init()
      setSession(true)
    }

    initUserProfile()
  }, [])

  if (hasSession === false) {
    return (<Loading />)
  }

  return (
    <Router>...</Router>
  )
 }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda