[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的更多相关文章
- [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, ...
- [Redux] Accessing Dispatch and State with Redux -- connect
If you have props and actions, you want one component to access those props and actions, one solutio ...
- 从零开始搭建一个react项目
Nav logo 120 发现 关注 消息 4 搜索 从零开始搭建一个react项目 96 瘦人假噜噜 2017.04.23 23:29* 字数 6330 阅读 32892评论 31喜欢 36 项目地 ...
- [Functional Programming ADT] Create a Redux Store for Use with a State ADT Based Reducer
With a well defined demarcation point between Redux and our State ADT based model, hooking up to a R ...
- [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 re ...
- 前端(十一):props、state及redux关系梳理
所谓状态机,是一种抽象的数据模型,是“事物发展的趋势”,其原理是事件驱动.广泛地讲,世界万物都是状态机. 一.状态机是一种抽象的数据模型 在react中,props和state都可以用来传递数据.这里 ...
- [React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
With our Redux implementation lousy with State ADT based reducers, it is time to hook it all up to a ...
- [Functional Programming ADT] Adapt Redux Actions/Reducers for Use with the State ADT
By using the State ADT to define how our application state transitions over time, we clear up the ne ...
- react+redux教程(六)redux服务端渲染流程
今天,我们要讲解的是react+redux服务端渲染.个人认为,react击败angular的真正“杀手锏”就是服务端渲染.我们为什么要实现服务端渲染,主要是为了SEO. 例子 例子仍然是官方的计数器 ...
随机推荐
- muduo网络库学习笔记(10):定时器的实现
传统的Reactor通过控制select和poll的等待时间来实现定时,而现在在Linux中有了timerfd,我们可以用和处理IO事件相同的方式来处理定时,代码的一致性更好. 一.为什么选择time ...
- 指令发email
win7下指令发送email:(telnet:不为内部指令时控制面板 -> 程序和功能 -> 打开或关闭Windows功能,如下“telnet客户端”) telnet smtp.sina. ...
- 从一个模板函数聊聊模板函数里面如何获得T的名字
写了个小程序,遇到点问题.总结总结,学习学习 #include<vector> #include<iostream> #include<typeinfo> usin ...
- C 字符串倒转,XCode中编译
正在学习ios开发,在前期学习c时,常规方法直接倒转数组的值,只能用于非中文字符,否则出现乱码, 在网上找了点资料,可能是 IDE不一致,一直得不到自己想要的值.花时间自己改了一下,正常通过 //字符 ...
- C语言之 短路原则
a=0;b=1 c=a&&(b=3) 最终c=0; b=1 因为从左至右进行时,若遇到运算符左边的操作数是 0(逻辑假),则停止运算. a=1;b=1;c=0; d=a||b||(c= ...
- Hbase Java API程序设计步骤
http://www.it165.net/admin/html/201407/3390.html 步骤1:创建一个Configuration对象 包含了客户端链接Hbase服务所需的全部信息: zoo ...
- BZOJ 2226 LCMSum
Description Given \(n\), calculate the sum \(LCM(1,n) + LCM(2,n) + \cdots + LCM(n,n)\), where \(LCM( ...
- MVC自学系列之二(MVC控制器-Controllers)
Controllers的职责 1.MVC模式中的Controllers的职责是对用户的输入做出响应,对用户的输入在实体上做一些变化.它关心的是应用的流动,处理传入的数据,并给相关的View提供数据 ...
- android 自定义命名空间
一.统一的用户界面是可以使得应用程序更友好.要做到用户界面的统一,我们就必须用到风格(style)和主题(theme).自定义一个View的方法步骤如下:1.首先,在values文件夹下定义一个att ...
- lc面试准备:Power of Two
1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...