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

123
Visualizações
JWT gets cleared when loading page

I am currently working on a project in react with registration and login, and am using JWT for authentication. It works well for the most part; the jwt gets stored in localStorage when the user is logged in. The issue comes when I introduce a button to log out.

<Link to="/">
    <button onClick={localStorage.clear()} style={{backgroundColor: "black"}} className="login-button">Log Out</button>
 </Link>

This log out button is rendered as soon as the user logs in, and I would expect it to clear local storage and return the user to the home page when clicking it. However, when putting localStorage.clear() in the onClick, the JWT token is cleared as soon as the user is logged in, even if the user hasn't clicked on log out. I have found after testing it that this is the code that seems to be the issue, and I can't really see why.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

React is executing localStorage.clear() on load because you're not giving a function to the onClick event but rather the execution/call of a function.

Try it this way:

<button onClick={() => {localStorage.clear()}} style={{backgroundColor: "black"}} className="login-button">Log Out</button>
about 4 years ago · Juan Pablo Isaza Relatório

0

when the page loads the button onClick will get triggered.

<Link to="/">
<button onClick={localStorage.clear} style={{backgroundColor: "black"}} className="login-button">Log Out</button>

this should resolve the issue

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