【发布时间】:2020-02-05 21:10:38
【问题描述】:
我在使用 Redux 时遇到问题,当我更改或重新加载当前页面时,我的商店会重置为初始状态。
在更改或重新加载页面后,我不想在我的网站上添加保持当前数字的计数器。我有一个“+”和一个“-”按钮,效果很好,但是当我重新加载我的页面时,值会重置为 0。
组件/layout.js:
const initialState = {
count: 0,
}
function reducer(state = initialState, action) {
console.log("reducer", state, action)
switch (action.type) {
//... some actions...
}
}
const store = createStore(reducer)
export default ({ children }) => (
<Provider store={store}>
...
</Provider>
预期:重新加载页面后计数器保持该值
实际:重新加载页面后计数器重置为 0
【问题讨论】:
-
Redux 状态存储在内存中。除非您自己保存和恢复它(服务器端、本地存储等),否则在刷新或页面更改后它将不可用。
-
Redux 状态不会在页面重新加载时保持不变。如果您希望它保持硬刷新,请查看this question
标签: javascript reactjs redux