1. Local Storage :
Keyword localStorage is used for local storage. Browser compatibility can be checked from official page Official Local Storage .
It’s very easy to use , as localStorage variable available in react without importing any library. It has four built in Function to store, retrieve and remove data.
-> To store value , it has setter function
Here here ‘key’ is key to store and retrieve data and ‘value’ is data. Data must be in string format, if there is JSON object then that needs to be convert in string. Below is an example:
Local Storage data will persist always even after closing browser. When we open browser then data will be there browser. You can find its source code from github Local Storage repository .
2. Session Storage :
Keyword sessionStorage is used for session storage. Browser compatibility can be checked from official page official session storage.
It is same as local storage, only the difference is that it has the expiration of data. Saved data will be empty on closing the browser however there will no intact on refreshing or tab closing. Session storage is have same function as of local storage. Source Code is available on GitHub
Below is the example of Session Storage.