Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How is session storage and local storage used in react?


Asked by Oaklynn Myers on Dec 07, 2021 React



The user session could be saved until the browser is closed. That's where you can use the native sessionStorage instead of the localStorage. The session storage is used in the same way as the local storage. How to Cache Data in React? Let's take the local storage usage in React one step further by deploying it as cache for search results.
Also Know,
Usually local storage is used for session features like saving user settings, favorite themes, auth tokens, etc. And usually, you read once from local storage on application start, and use a custom hook to update its keys on related data change.
Furthermore, The sessionStorage object stores data only for a session, meaning that the data is stored until the browser (or tab) is closed. Data is never transferred to the server.
Indeed,
There are different recommendation for session management in react apps. First of all we will talk about these options and then will go through to one I have used. Use a closure wrapper to maintain user information. Once user is logged in we can store that information in cookies/local-storage and can be retrieve in app components.
Additionally,
This is the path used when the session is rejected or doesn’t exist. Suppose a new user attempts to access a secured page by browsing to the URL directly. Because there is no session, the user will be re-routed to /login by default, or /home in the example above.