I'm trying to use localStorage on a NextJS static site but running into the localStorage is not defined error.
My code for the component where the error shows, is:
export default function Category() {
const [cart, setCart] = useState(
localStorage.getItem("myCart") || ""
);
useEffect(() => {
localStorage.setItem("myCart", cart);
}, [cart]);
function addItem() {
setCart("item")
}
return (
<>
<button onClick={addItem}>Add</button>)
</>
);
}
Any idea if it's possible to use localStorage on a NextJS static site or if I'm doing something wrong?
Thanks!