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

311
Vistas
Axios instance not getting the token from the Local storage on the first attempt || React JS

I created the axios instance and storing the token in the localStorage at the time of login but for some reasons token is not loaded in the localstorage whenever the api calls and got an error from the backend. But then if i manually reload the page the api gets call and now instance is getting the token and data is loaded and works fine. How to resolve it ????? Here is the code:

export const instance2 = axios.create({

baseURL: 'http://127.0.0.1:8000',  
headers: {
    Authorization: `Bearer ${localStorage.getItem('token')}`
}

});

I'm using this instance2 in each page but it not works on first attempt.

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

0

it is because instance2 is not a function and is not triggered every api call. You should convert that to function.

Example:

export const instance2 = () => {
   return axios.create({
      baseURL: 'http://127.0.0.1:8000',  
      headers: {
      Authorization: `Bearer ${localStorage.getItem('token')}`
     }
  });
 
}

Example usage:

instance2().get('url')
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