How to remove data from sessionstorage in React js?

Solution To remove data from sessionstorage in react js, use sessionStorage.removeItem() method it will delete the key and value from sessionstorage. sessionStorage.removeItem() The removeItem() method of the Storage interface, when passed a key name, will remove that key from the given Storage object if it exists. Snippet In this snippet, we will delete the key and value sessionstorage using sessionStorage.removeItem() method. sessionStorage.removeItem('bgcolor'); Example In this example, we will store some values in local storage and then use the sessionStorage....

November 28, 2022 · 1 min · The Unknown Developer

How to store object in sessionstorage in React js?

Solution To store an object in sessionstorage in react js, use sessionStorage.setItem() with the JSON.stringify() method it will convert an object into a string and store it in sessionstorage. sessionStorage.setItem() The setItem() method of the Storage interface, when passed a key name and value, will add that key to the given Storage object, or update that key’s value if it already exists. JSON.stringify() The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified....

November 27, 2022 · 2 min · The Unknown Developer

How to use sessionstorage in react js functional component?

Solution To use sessionstorage in react js functional component, you have to use sessionstorage with react hooks depending upon the situation. Snippet In this snippet, we will use react hooks with sessionstorage for the following points. Get data from sessionstorage when the component load Store data on sessionstorage when the component load Store data on sessionstorage when a state change Get data from sessionstorage when the component load On a load of components, sometimes we need to fetch data from local storage and store it in a state to show in the UI....

November 27, 2022 · 2 min · The Unknown Developer

How to clear sessionstorage in React js?

Solution To clear sessionstorage in react js, use sessionStorage.clear() method it will delete all keys and values from sessionstorage. sessionStorage.clear() The clear() method of the Storage interface clears all keys stored in a given Storage object. Snippet In this snippet, we will clear all keys sessionstorage using sessionStorage.clear() method. sessionStorage.clear(); Example In this example, we will store some values in sessionstorage after then use sessionStorage.clear() method to delete all values....

November 26, 2022 · 1 min · The Unknown Developer

How to store array in sessionstorage in React js?

Solution To store array in sessionstorage in react js, use sessionStorage.setItem() with JSON.stringify() method it will convert array into string and store in sessionstorage. sessionStorage.setItem() The setItem() method of the Storage interface, when passed a key name and value, will add that key to the given Storage object, or update that key’s value if it already exists. JSON.stringify() The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified....

November 26, 2022 · 2 min · The Unknown Developer

How to get data from sessionstorage in React js?

Solution To get data from sessionstorage in react js, use sessionStorage.getItem() method it will return stored value from sessionstorage. sessionStorage.getItem() The getItem() method of the Storage interface, when passed a key name, will return that key’s value, or null if the key does not exist, in the given Storage object. Snippet In this snippet, we will get some value from localstorage using sessionStorage.getItem() method. const token = sessionStorage.getItem("access_token"); console.log(token); // dXNlckBleGFtcGxlLmNvbTpzZWNyZXQ= Example In this example, we will get token from sessionstorage and show in the console and page....

November 25, 2022 · 1 min · The Unknown Developer

How to get json from sessionstorage in React js?

Solution To get json from sessionstorage in react js, use sessionStorage.getItem() method it will return JSON string then use JSON.parse() method to convert it into Object. sessionStorage.getItem() The getItem() method of the Storage interface, when passed a key name, will return that key’s value, or null if the key does not exist, in the given Storage object. Snippet In this snippet, we will get some JSON value from sessionstorage using sessionStorage....

November 25, 2022 · 1 min · The Unknown Developer