I’m encountering an issue with sessionStorage in my React app. I’m using the "use client" pragma to ensure certain code runs only on the client side, but I’m still getting a ReferenceError.
Here’s my code:
"use client";
import { useEffect, useState } from "react";
import { ProgressBar } from "staysure-component-library";
import Footer from "../components/organisms/footer/Footer";
import HomePage from "../components/pages/Home/HomePage";
import Styles from "./homePage.module.css";
function Page() {
const [test, setTest] = useState(false);
let hideFooter = sessionStorage.getItem("footer") === "true";
useEffect(() => {
if (!hideFooter) {
sessionStorage.setItem("footer", "true");
}
}, []);
return (
<div className="bg-surface-neutral-option-2">
<header>
<ProgressBar value={10} />
</header>
<div>
<HomePage />
{hideFooter ? null : (
<footer>
<Footer />
</footer>
)}
</div>
</div>
);
}
export default Page;
2
Answers
error is from here:
client components first gets prerendered on the server to show the non-intearctive page html to the user as quick as possible. during this prerender on the server,
sessionStorage
is not defined.this will get rid of the error.
this must work I had the same problem with my app running in nextjs this helped me