immutable.js使用总结
1. immutable相当于 JSON.parse 和 JSON.stringify;
2.引入redux中,除了
在最外层 reducer中 import { combineReducers } from 'redux-immutable';
涉及到修改 (1)reducer 两个文件 (2)组件
2.1 对于reducer:
首先对默认状态:
import { fromJS } from 'immutable';
const defaultState = fromJS({
'requireFlag':true
})
对于简单的处理state,直接return即可:
case actionTypes.CHANGE_MORE_FLAG:
return state.set('requireFlag',!state.get('requireFlag'));
对于需要复杂处理的,一般将state传入自定义函数,最后返回:
export default (state=defaultState, action)=>{
switch (action.type) {
case actionTypes.INIT_LISTS:
return handleInitList(state,action);
}
}
const handleInitList = (state,action)=>{
state.get('nav').map((item,index)=>{ //所有涉及到获取state,使用get方法,这里改变了state
item.checked = false;
});
state.set('page',1); //后续改变其他值,单个是使用 state.set('page',1)
return state.merge({ //改变多个值,需要使用merge函数,最后一定要 return出 merge函数
activeIndex:action.tabIndex,
cardId:action.cardId,
})
//return state; 不要使用merge之后,在这里return state
}
数组合并:
state.get('lifeRights').concat(action.initData.lifeRights);
2.2 组件:
function mapStateToProps(state, ownProps) {
return {
userStatus:state.getIn(['bottom','userStatus']), //注意此处有 中括号,第一个 reducer的名字
}
}
如果该reducer中 只有一层级,如reducer中的状态定义为:
const defaultState = fromJS([])
则在组件中调用使用 get : rightsList: state.get('hotRight')
immutable.js使用总结的更多相关文章
- 深度浅出immutable.js
这篇文章将讲述immutable.js的基本语法和用法. 1.fromJs() Deeply converts plain JS objects and arrays to Immutable Ma ...
- 大话immutable.js
为啥要用immutable.js呢.毫不夸张的说.有了immutable.js(当然也有其他实现库)..才能将react的性能发挥到极致!要是各位看官用过一段时间的react,而没有用immutabl ...
- Immutable.js – JavaScript 不可变数据集合
不可变数据是指一旦创建就不能被修改的数据,使得应用开发更简单,允许使用函数式编程技术,比如惰性评估.Immutable JS 提供一个惰性 Sequence,允许高效的队列方法链,类似 map 和 f ...
- Immutable.js尝试(node.js勿入)
最近做一些复杂html常常需要在页面做一些数据处理,常常在想如果 js有list 这种数据结构多少,今天逛github时 发现有Immutable.js 这个项目https://github.com/ ...
- React+Immutable.js的心路历程
这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...
- [Javascript] Creating an Immutable Object Graph with Immutable.js Map()
Learn how to create an Immutable.Map() through plain Javascript object construction and also via arr ...
- [Javascript] Manage Application State with Immutable.js
Learn how Immutable.js data structures are different from native iterable Javascript data types and ...
- [Immutable.js] Working with Subsets of an Immutable.js Map()
Immutable.js offers methods to break immutable structures into subsets much like Array--for instance ...
- [Immutable,js] Iterating Over an Immutable.js Map()
Immutable.js provides several methods to iterate over an Immutable.Map(). These also apply to the ot ...
- [Immutable,js] Immutable.Record() as data models
The Immutable.js Record() allows you to model your immutable data much like you would model data wit ...
随机推荐
- 数独_erlang解题代码
前几天LP玩数独,玩到大师级各种被虐,我看了看说,分分钟帮你做出来, 结果当然没有做出来. 于是上网搜了下数独的解题代码,看了下C的代码,大多是递归之类的(如http://blog.sina.com. ...
- Spring boot连接MongoDB集群
主要问题是:MongoDB集群分为复制集(replicaSet)与分片集(shardingSet),那么如何去连接这两种集群: 参考官方文档,我使用了最通用的方法:通过构造connection str ...
- mysql以zip安装,解决the service already exists
mysql以zip安装, mysqld -install 报错:The service already exists 原因是之前安装了以后卸载了,服务没删掉. 解决方法: sc query m ...
- sublime-代码提示
py的话安装这个插件: Anaconda user配置 { "python_interpreter":"F:/PY3/python.exe", "su ...
- 再次提供一个纯粹通过pl/sql解析json的方法。
在github上面有一个叫pljson的项目,该项目就是用pl/sql 来解析json的. 项目地址:pljson(需翻|强),如果翻不了强的同学,我在国内克隆了一个副本,不定期同步更新 pljson ...
- poj 3254Corn Fields (入门状压dp)
Farmer John has purchased a lush ≤ M ≤ ; ≤ N ≤ ) square parcels. He wants to grow some yummy corn fo ...
- 2162112375 Week04-面向对象设计与继承
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 对象.类.封装性.静态属性.静态方法.重载.继承.多态 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现 ...
- [Codeforces778E]Selling Numbers
Problem 给一个由问号和数字组成的数字串A(问号表示任一数字). 再给定n个数字Bi,和0~9的数字的价值. F(x)表示x各个位数上的价值和.问A为何值时,sum(F(Bi+A))的值最大为多 ...
- nginx搭建以及其配置文件
nginx搭建: 参考link:https://blog.csdn.net/wxyjuly/article/details/79443432 nginx配置文件详解: 参考link:https://w ...
- 50个常用的Linux命令(二)sed
[root@localhost cee]# echo this thisthisthis |sed 's/this/THIS/g'THIS THISTHISTHIS[root@localhost ce ...