[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.
时间: 2024-09-28 21:49:44

[Redux] Supplying the Initial State的相关文章

[Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined

For example we have a component, it needs to call 'react-redux' connect function. import { compose, curry, option, propPath } from '../js/helper' const FilterButton = ({ active, onClick }) => { const classes = classnames('filterButton', { 'filterButt

[Functional Programming ADT] Initialize Redux Application State Using The State ADT

Not only will we need to give our initial state to a Redux store, we will also need to be able to reset our state at any time by dispatching an action. We can get the best of both worlds by having a function that will return an object with all of our

[Redux] Composition with Objects

For example, current we have those todos: { todos: [ { completed: true, id: 0, text: "Learn Redux" }, { completed: false, id: 1, text: "Go shopping" }], } And now what we want to do is add a 'visibilityFilter' prop to the object, compo

Redux系列x:源码解析

写在前面 redux的源码很简洁,除了applyMiddleware比较绕难以理解外,大部分还是 这里假设读者对redux有一定了解,就不科普redux的概念和API啥的啦,这部分建议直接看官方文档. 此外,源码解析的中文批注版已上传至github,可点击查看.本文相关示例代码,可点击查看. 源码解析概览 将redux下载下来,然后看下他的目录结构. npm install redux 这里我们需要关心的主要是src目录,源码解析需要关心的文件都在这里面了 index.js:redux主文件,主

正式学习React(四) ----Redux源码分析

今天看了下Redux的源码,竟然出奇的简单,好吧.简单翻译做下笔记: 喜欢的同学自己可以去github上看:点这里 createStore.js 1 import isPlainObject from 'lodash/isPlainObject' 2 import $$observable from 'symbol-observable' 3 4 /** 5 * These are private action types reserved by Redux. 6 * For any unkno

从零开始的Android新项目10 - React Native & Redux

本篇来讲讲 React Native 和 Redux,和其他一上来就啪啪啪丢上来一堆翻译的东西不同,本文会从简单的例子入手,让大家能快速地明白 React Native 是什么,Redux 和常见的 MVC.MVP 等有什么区别,怎么去组织一个 Redux 架构的 React Native 项目. 为避免大家还没入门就放弃,预计下一篇才会从我们项目中的实践出发,讲讲更复杂的应用场景. 什么是React Native React Native 使你能够基于 JavaScript 和 React 在

react+redux教程(六)redux服务端渲染流程

教程目录 react+redux教程(一)connect.applyMiddleware.thunk.webpackHotMiddleware react+redux教程(二)redux的单一状态树完全替代了react的状态机? react+redux教程(三)reduce().filter().map().some().every()....展开属性 react+redux教程(四)undo.devtools.router react+redux教程(五)异步.单一state树结构.compo

Redux:store

Store是一个对象.他有如下职责: 1.存放state 2.对外提供访问state的接口: getState() 3.允许state更新:dispatch(action) 4.注册监听器: subscribe(listener) 5.注销监听器,通过subscribe返回的函数 redux所在的应用只能有一个store实例,如果想将state操作分解成多个逻辑,只能将Reducer的代码分解为多个部分,以函数调用的方式提取出来处理各自的逻辑. 当我们已经有一个处理state的Reducer函数

redux源码解析,函数式编程

提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript>中,作者批评了java那种任何东西都用对象来写程序的方式,提倡了这种函数式编程. 之前看过一些函数式编程的例子(以下简称fp).提到fp会想到underscore和lodash,你会看到lodash的包中,唯一一个文件夹就是fp,里面是fp相关的函数. 在redux中,也是运用了很多fp的函数.其实在写js中