【发布时间】:2016-11-20 05:28:44
【问题描述】:
我有一个工作正常的简单减速器状态。 M减速机在下方。
const initialState =
{
plan: [
{
id: 0,
text: 'Submit math assignment at noon.',
completed: false
}
]
}
function mytodo(state = initialState, type) {
switch(type) {
...
default:
return state;
}
}
我的应用程序运行良好。然后将一些本地存储数据连接到我的状态。我的状态现在返回未定义。
const storageItems = JSON.parse(localStorage.getItem('plan'));
function mytodo(state = initialState, type) {
switch(type) {
...
default:
console.log('the storage data is', storageItems);
return storageItems ? state.plan.concat(storageItems) : state;
}
}
我确认上面的存储项目有数据,但我的减速器将未定义的计划返回给我的组件。然后我将其更改为。
const storageItems = JSON.parse(localStorage.getItem('plan'));
if(storageItems) {
initialState = initialState.todos.concat(storageItems);
console.log('the states are', initialState);
}
function mytodo(state = initialState, type) {
switch(type) {
...
default:
return state;
}
}
并将 initialState 更改为 let。它仍然返回未定义。上面控制台的初始状态返回我需要的完整结果。但是,如果我更新了 initialState,它不会将值返回给我的组件。请问我做错了什么?我该如何解决这个问题?任何帮助将不胜感激。
【问题讨论】:
标签: javascript redux