![]() |
To manage Loacal Storage and Session Storage, we can use hooks like useEffect and useState provided by React. Managing local or session storage is a repetitive task, so it is a good practice to create a custom hook that manages the storage properly. In this article, I’ll cover the whole process of managing storage with the best industry standard practice. Table of Content To see some working examples we need to first setup our react app. . To setup a basic React App, refer to Folder Structure for a React JS Project article. Now, we can create custom hooks. So, There are basically three operations that we need to perform:
1. useLocalStorage():Create a state that will store either the data from the local storage if present otherwise it will store initial value.Then everytime, when key or value changes we will the new key-value pair in the local storage. That’s it. Example: Below is the code example for useLocalStorage():
Output: ![]() useLocalStorage() 2. useSessionStorage():Create a state that will store either the data from the session storage if present otherwise it will store initial value.Then everytime, when key or value changes we will the new key-value pair in the session storage. That’s it. Example: Below is the code example for useSessionStorage():
Output: ![]() Session Storage |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |