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

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