Search code examples
sessionbrowserreactjsbrowser-api

How do I determine if a new ReactJS session and/or Browser session has started?


Each time a user navigates to an URL I plan on storing the "lastUrl" in localStorage. When they return to the site, under some conditions which are not important, I want to immediately transition them to the lastUrl that they were visiting.

I'm using ReactJS but not sure if the solution would be ReactJS specific or just using standard Browser APIs.

Is there a Browser API or ReactJS specific pattern for doing this?

This Q&A seems flaky: How to determine if the browser session just started or if it's a subsequent click?


Solution

  • Depending on what counts as a "session" in your app, you could use sessionStorage instead - one of the key things is that a new tab counts as a new session for sessionStorage.