gpt4 book ai didi

javascript - 使用 localstorage Redux + Next.js 加载初始状态

转载 作者:行者123 更新时间:2023-12-05 00:36:43 28 4
gpt4 key购买 nike

我正在尝试从 localstorage 加载初始状态以创建存储,但出现此错误:ReferenceError: localStorage is not defined我的店铺:

const store = createStore(reducer, {
userInfo: {
isLogged: false,
items: localStorage.getItem("@items") || []
}
})

最佳答案

本地存储未在下一个 js 中定义,因为它呈现在服务器端
实现你的 Redux 使用 Next js 的这个样板。
Redux Persist example

This example shows how to integrate Redux with the power of ReduxPersist in Next.js.

With the advantage of having a global state for your app using redux.You'll also require some of your state values to be available offline.There comes redux-persist using which you can persist your states inbrowser's local storage. While there are various ways of persistingyour states which you can always find in there documentation. This isan example of how you can integrate redux-persist with redux alongwith Next.js's universal rendering approach.

关于javascript - 使用 localstorage Redux + Next.js 加载初始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63276931/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com