[Redux] Supplying the Initial State
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Redux] Supplying the Initial State相关的知识,希望对你有一定的参考价值。
We will learn how to start a Redux app with a previously persisted state, and how it merges with the initial state specified by the reducers.
The initial state of store is defined by the rootReducers:
const todoApp = combineReducers({
todos,
visibilityFilter,
});
And we use ‘todoApp‘ to create store:
const store = createStore(todoApp);
So the initial state should be default value of each reducer‘s state:
const todos = (state = [], action) => { ...
const visibilityFilter = (state = ‘SHOW_ALL‘, action) => { ...
If we want to show some persosted data as initial state, we can pass the persisted data as a second args to ‘createStore()‘ function:
const persistedState = { todos: [ { id: 0, text: "Redux", completed: false } ] }; const store = createStore(todoApp, persistedState);
So the rules are:
- If there is persisted data you want to display, use this second args, otherwise use ES6 default param
- Once persisted data is passed in, it will overwrite the default params.
以上是关于[Redux] Supplying the Initial State的主要内容,如果未能解决你的问题,请参考以下文章
[Redux] Persisting the State to the Local Storage
[Redux] Normalizing the State Shape
[Redux] Refactoring the Entry Point
[Redux] Adding React Router to the Project
redux-devtools安装 以及redux No store found. Make sure to follow the instructions.解决