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

125
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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