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

349
Views
In react, localStorage and window.localStorage both null when fetching auth-token

In the App.js of our react application, created several years ago using create-react-app, we check to see if a user is already logged in via a useEffect() that checks localStorage for a prior authentication token. Sometimes, we run into errors. Directly posting sentry error seems like a lazy way to post on this issue, however it also seems like the clearest and most effective way to articulate the issue:

enter image description here

Because we are using the ternary operator, presumably the error is being thrown for window.localStorage.getItem('auth-token'), i.e window.localStorage seems to be null. My plan to fix is to simply also check that window.localStorage is not null:

let token = localStorage ? localStorage.getItem('auth-token') : (window.localStorage ? window.localStorage.getItem('auth-token') : null)

I think this solution will work okay, even if not the perfect solution. My main question is why is it that localStorage and window.localStorage are sometimes null - shouldn't these always have some value even if an empty string? Also, is there a more proper way to get auth-tokens from localStorage that I'm missing here? The double ternary operators, and using window.localStorage as the fallback for lcoalStorage, don't quite seem 100% proper.

about 4 years ago · Juan Pablo Isaza
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!