小能豆

如何在 redux 中设置初始状态

javascript

我正在尝试弄清楚如何在 redux 中为商店设置初始状态。我使用https://github.com/reactjs/redux/blob/master/examples/todos-with-undo/reducers/index.js作为示例。我尝试修改代码,使 todos 具有初始化值。

const todoApp = combineReducers({
  todos,
  visibilityFilter
}, {
  todos: [{id:123, text:'hello', completed: false}]
})

遵循文档:http://redux.js.org/docs/api/createStore.html

但它不起作用,我也不太清楚为什么。


阅读 53

收藏
2024-06-07

共1个答案

小能豆

它需要是第二个参数createStore

const rootReducer = combineReducers({
  todos: todos,
  visibilityFilter: visibilityFilter
});

const initialState = { 
  todos: [{id:123, text:'hello', completed: false}] 
};

const store = createStore(
  rootReducer, 
  initialState
);
2024-06-07