Sometimes, we want to initialize and using session storage in React and JavaScript.
In this article, we’ll look at how to initialize and using session storage in React and JavaScript.
How to initialize and using session storage in React and JavaScript?
To initialize and using session storage in React and JavaScript, we can call the sessionStorage
methods.
For instance, we write:
import React from "react";
export default function App() {
React.useEffect(() => {
sessionStorage.setItem("key", "value");
console.log(sessionStorage.getItem("key"));
}, []);
return <></>;
}
to call sessionStorage.setItem
with the key and value of the entry we want to store in session storage respectively.
Then we call sesessionStorage.getItem
with the key to get the entry with the given key.
Conclusion
To initialize and using session storage in React and JavaScript, we can call the sessionStorage
methods.