Immutable数据类型
- List 有序可重复的列表,对应于Array,列表是完全持久化的,
- Map 无序键值对集合,对应与Object
- Set 无序且不可重复的键值对
贴个网址
Immutable
-
immutable是一种持久化数据。一旦被创建就不会被修改。修改immutable对象的时候返回新的immutable。但是原数据不会改变。
-
fromJS:将普通的对象和数组转换为immutable
-
fromJS有两个参数,
-
-
toJS:将immutable转换为js
-
Map
-
delete:返回一个不包括这个键的新 Map
-
deleeIn: 返回已删除此 keyPath 处的值的新 Map。如果 keyPath 中的任何键不存在,则不会发生任何更改。
-
merge:
1. Map的键都相同,将使用最后一个要合并的Map中的重复键的值。
2. (类型合并为最后一个Map的类型) -
mergeDeep:
1. 无论嵌套层次结构的深度,都会被递归合并
2. (类型合并为原Map的类型) -
List
-
delete: This is synonymous with
list.splice(index, 1)
. -
deleteIn:返回已删除此 keyPath 处的值的新列表。如果 keyPath 中的任何键不存在,则不会发生任何更改。
-
获取immutable中的值
-
get和getIn
实际使用
-
获取某值
state.get('key')
: 获取List或者某个key值
state.getIn(['key', index)
: 获取List中index索引的值
state.getIn(['key', 'key2')
: 获取Map中key2的值 -
设置值
state.setIn(['key', itemIndex, 'key2'], 0)
: 更新key中索引为itemIndex中的key2的值为0
state.setIn(['key', itemIndex], fromJS({ ...itemFind }))
: 更新key中索引为itemIndex的值为处理为Immutable Map数据类型的itemFind
state.updateIn(['key', 'key2'], key2=> key2.concat(fromJS([])));
: 更新key中key2,对key2进行处理
newState = newState.updateIn(['key', 'key2', index], () => item)
: 更新key中的key2中索引为index的值为item -
查询
state.hasIn(['key', 'key2'])
: 查询key中是否有key2 -
转化为Immutable数据类型
Immutable.fromJS(value)
Tips
- fromJS()和toJS进行深度数据转换,开销较大,尽可能避免使用
Reference website
https://blog.youkuaiyun.com/qq_42941302/article/details/111834035
https://www.jianshu.com/p/728a1afce96d
https://zhuanlan.zhihu.com/p/169610450
https://www.jianshu.com/p/de7115867c3c