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

323
Visualizações
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 Respostas
Responde à pergunta

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 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