You can use session storage with hooks. Since it is shared in the context, it will be re-rendered when the dependencies are updated.
npm install react-sessionstorage
import React from "react";
import ReactDOM from "react-dom";
import {
SessionStorageProvider,
useSessionStorage,
} from "react-sessionstorage";
const Component = () => {
return (
<SessionStorageProvider>
<ChildComponent />
</SessionStorageProvider>
);
};
function ChildComponent() {
const [
sessionStorages,
setSessionStorage,
removeSessionStorage,
] = useSessionStorage(["test"]);
const setItem = () => {
setSessionStorage("test", "hoge");
};
return <button onClick={setItem}>{sessionStorages.test}</button>;
}
ReactDOM.render(<Component />, document.getElementById("app"));
[sessionStorage,setSessionStorage,removeSessionStorage]
string[]
Put the session storage key in the array.
Rerendered when the sessionStorage keys contained in the array are updated.
setSessionStorage("sesssionStoragekey","value")
Set sessionStorage.
removeSessionStorage("sesssionStoragekey")
remove sessionStorage.