0

I am using this code,

useEffect(() => {
window.addEventListener("beforeunload",() => {
localStorage.removeItem("token")
})
})

but my requirement is to clear this token only when all tabs get closed or the browser itself gets closed, but this event is occurring for even page refresh, so is there any possible way to block this part of code when the page gets refreshed and only execute when tabs or browser gets closed.

Any help would be highly appreciable, thanks in advance!!!

1 Answer 1

1

You can try using sessionStorage. It will automatically clear token after closing the tab but it works only for one tab.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.