码迷,mamicode.com
首页 > 其他好文 > 详细

[Redux] Supplying the Initial State

时间:2016-06-03 06:27:16      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5554960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!