zoukankan      html  css  js  c++  java
  • [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.
  • 相关阅读:
    13 内建属性 _getattribute_ 内建函数
    12 垃圾回收GC
    11 元类
    12 动态语言 __slots__
    11 作用域
    10 带参数的装饰器 通用装饰器 类装饰器
    9 装饰器
    8 闭包
    6 生成器 yield 协程
    cmd常用命令
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5554960.html
Copyright © 2011-2022 走看看