Dante1021
Dante1021

Reputation: 374

How to detect Page Refresh in React

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

Upvotes: 0

Views: 139

Answers (1)

Kirill Tarasov
Kirill Tarasov

Reputation: 11

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

Upvotes: 1

Related Questions