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

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

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