我正在使用 NextJS 12。我正在尝试获取本地存储对象。当我在 getServerSideProps
中使用 localstorage 时,会收到类似 ReferenceError: localStorage is not defined
的错误。我也尝试在函数之外使用它,但仍然出现此错误。有没有办法在 getServerSideProps
中使用它。
export async function getServerSideProps({ query }) {
const id = query.id;
const getData = JSON.parse(localStorage.getItem("form"));
console.log(getData)
return {
props: {},
}
另一种方法是使用 cookie 而不是使用
localstorage
,我在开发最后一个应用程序时遇到了同样的问题,我使用nookies
包解决了它Nookies: A collection of cookie helpers for Next.js