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

361
Visualizações
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 à 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