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 ...
随机推荐
- 若快打码平台python开发文档修改版
一.打码的作用 在进行爬虫过程中,部分网站的登录验证码是比较简单的,例如四个英文数字随机组合而成的验证码,有的是全数字随机组成的验证码,有的是全中文随机组成的验证码.为了爬虫进行自动化,需要解决自动登 ...
- C#中get和set
释一: 属性的访问器包含与获取(读取或计算)或设置(写)属性有关的可执行语句.访问器声明可以包含 get 访问器或 set 访问器,或者两者均包含.声明采用下列形式之一: get {} set {} ...
- Nginx教程--02.Nginx虚拟主机的配置
1.Nginx虚拟主机的配置 1.1 在conf目录下,使用命令 : vim nginx.conf 对上图解释: //全局区 worker _processes 1; //表示当前有1个工作的子进程, ...
- java poi excel操作 下拉菜单 及数据有效性
1 private InputStream updateTemplateStyleHSSF(InputStream inputStream,CsCustCon csCustCon) throws IO ...
- 解决Android studio生成H文件时报找不到类文件错误
今天整理思路时觉得在native直接调用java的方法显示这个办法挺不错 于是就用到了生成H文件 可能我的编译环境和他们的不一样 网上的教程如下 javah -d ../jni com.jm.prom ...
- Vue通过id跳转到商品详情页
首页列表: 在这里我用a标签进行跳转,在vue里面使用<router-link></router-link> <router-link :to="{path:' ...
- java子类继承父类的方法(代码简略版)
父类:public class Subjects { public void b() { System.out.println("学科"); } public void a(){ ...
- golang 缺少逗号报错问题
一个逗号引发的语法报错问题:syntax error: unexpected newline, expecting comma or }或者missing ',' before newline in ...
- Java覆盖
Java的覆盖: 源代码: package dijia;class Parent1{ void f() { System.out.println("迪迦奥特曼1"); } void ...
- leetcode python 006
## 改为z型字符串def change_to_z(s,n): ## 字符串不能生成完整的区,用空格补全 b=len(s)%(2*n-2) if b!=0: s+=' ...