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

122
Views
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 answers
Answer question

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 Report

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 Report
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!