Before starting and going into practical project on LocalStorage, Let's discuss about what is local storage and why we need to use this in ReactJS for web development
LocalStorage is a local web storage object to store the data on the user’s computer locally or you can say the local storage is used to store temporary into the browser memory which means the stored data will be saved across browser sessions so that we can use that data in future like we do in todo list app which run in mobile which also using React and the data stored has no expiration time until you delete that data.
in this post, I’ll create a custom hook to use LocaleStorage with ReactJS.
I will create a simple example to read and write data into local storage using reactjs.
and while going through my post if you found it difficult to understand or you have any doubts then contact me on any social media and to watch on this particular topic. So Let's start
Here i will take Name, Email and Password from user and then save it to local storage and then retrieve that data from local storage.
let's create simple form which consists of 3 fields which are Name, Email and Password and a button for submit our data to local
Note: It is not necessary to use button to save data in local but when you creating form then its looks great if you provide a button which will help user to save their data to local storage of browser
Now let's Go to Code
- Using useState to save input value in variable which will used to transfer that value to storage
- Creating a function named (handle) so that after click on button this function will be run and into this function we are saving input data from user to local storage with the help of localStorage.setItem("name",name") where localStorage is predefined keyword and setItem with this is also predefined
here i am giving value to this form user input using state.