[Javascript] Simplify Creating Immutable Data Trees With Immer
Immer is a tiny library that makes it possible to work with immutable data in JavaScript in a much more straight-forward way by operating on a temporarily draft state and using all the normal JavaScript API's and data structures. The first part of the lesson explains how to use Immer, and the second part of the lesson shows you how it can be used to simplify, for example, Redux reducers.
Immer has a few unique features:
- Supports arbitrary, deep modifications in immutable data trees
- Strongly typed
- Uses JavaScript native data structures, no new API to learn
- Automatically freezes any data that is produced
The whole point of Immer is that you can wrap you reducer function with the function immer provide, let's call 'produce'. When you call it, if you don't do anything, it just return your previous state, if you do any partial mutation, if will keep original object untouched, instead creating immutable version return to you. therefore make code much simpler.
// Before const byId = (state, action) => {
switch (action.type) {
case RECEIVE_PRODUCTS:
return {
...state,
...action.products.reduce((obj, product) => {
obj[product.id] = product
return obj
}, {})
}
default:
return state
}
}
// After import produce from 'immer' const byId = (state, action) =>
produce(state, draft => {
switch (action.type) {
case RECEIVE_PRODUCTS:
action.products.forEach(product => {
draft[product.id] = product
})
}
})
Or an improved version:
import produce from 'immer' const byId = produce((draft, action) => {
switch (action.type) {
case RECEIVE_PRODUCTS:
action.products.forEach(product => {
draft[product.id] = product
})
}
})
[Javascript] Simplify Creating Immutable Data Trees With Immer的更多相关文章
- [Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into Immutable Data
Immutable.js offers the fromJS() method to build immutable structures from objects and array. Object ...
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
To demonstrate the difference between mutability and immutability, imagine taking a drink from a gla ...
- 17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files
17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files 如果数据库是大的, 复制raw 数据文 ...
- 17.1.1.5 Creating a Data Snapshot Using mysqldump 创建一个快照使用mysqldump:
17.1.1.5 Creating a Data Snapshot Using mysqldump 创建一个快照使用mysqldump: 创建一个数据快照的方式是使用mysqldump 工具来备份所有 ...
- Use JavaScript to Export Your Data as CSV
原文: http://halistechnology.com/2015/05/28/use-javascript-to-export-your-data-as-csv/ --------------- ...
- Basic Model Theory of XPath on Data Trees
w https://openproceedings.org/2014/conf/icdt/FigueiraFA14.pdf From a database perspective, however, ...
- [Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types
Immutable.js provides several conversion methods to migrate one structure to another. Each Immutable ...
- [Javascript] Querying an Immutable.js Map()
Learn how to query an Immutable.Map() using get, getIn, has, includes, find, first and last. These a ...
- asp and javascript: sql server export data to csv and to xls
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <% //塗聚文 //20131021 functio ...
随机推荐
- Python:Fatal error in launcher: Unable to create process using 问题排查
cmd> django-admin 回车Fatal error in launcher: Unable to create process using '"c:\users\admin ...
- HAVING使用子查询
HAVING使用子查询 //查询各部门平均工资,显示平均工资大于 //公司整体平均工资的记录 select deptno,avg(sal) from emp group by ...
- java中的接口中的方法
题目如下:(多选题)请选择以下接口定义正确的方法() A:public static void main (String[] args); B:private void test(); C:publi ...
- 洛谷 P2559 [AHOI2002]哈利·波特与魔法石
P2559 [AHOI2002]哈利·波特与魔法石 题目描述 输入输出格式 输入格式: 文件中第一行有七个数,分别是 S1. S2 . …. S7 :第二行有两个数,依次分别是起点城市 i 和终点城市 ...
- skimage的安装,scikit-image
在mac上面的安装: pip install -U scikit-image
- POJ 1281 MANAGER
MANAGER Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description O ...
- JDBC-Statement 对象
Statement 对象 一旦我们获得了数据库的连接,我们就可以和数据库进行交互.JDBC 的 Statement,CallableStatement 和 PreparedStatement 接口定义 ...
- Post请求方式长度參数过长导致參数为空
Post提交方式本身对于參数的长度没有限制,HTTP协议也没有限制. 可是今天在做一个web项目的时候碰到一个问题,当要提交的表单内容达到一定大小时,发现后台代码接收到的參数为空. 查询了一下.发现是 ...
- bzoj4823: [Cqoi2017]老C的方块(最小割)
4823: [Cqoi2017]老C的方块 题目:传送门 题解: 毒瘤题ORZ.... 太菜了看出来是最小割啥边都不会建...狂%大佬强强强 黑白染色?不!是四个色一起染,四层图跑最小割... 很 ...
- 中文版 Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks 摘要 最先进的目标检测网络依靠区域提出算法 ...