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的更多相关文章

  1. [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 ...

  2. [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures

    To demonstrate the difference between mutability and immutability, imagine taking a drink from a gla ...

  3. 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 数据文 ...

  4. 17.1.1.5 Creating a Data Snapshot Using mysqldump 创建一个快照使用mysqldump:

    17.1.1.5 Creating a Data Snapshot Using mysqldump 创建一个快照使用mysqldump: 创建一个数据快照的方式是使用mysqldump 工具来备份所有 ...

  5. Use JavaScript to Export Your Data as CSV

    原文: http://halistechnology.com/2015/05/28/use-javascript-to-export-your-data-as-csv/ --------------- ...

  6. Basic Model Theory of XPath on Data Trees

    w https://openproceedings.org/2014/conf/icdt/FigueiraFA14.pdf From a database perspective, however, ...

  7. [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 ...

  8. [Javascript] Querying an Immutable.js Map()

    Learn how to query an Immutable.Map() using get, getIn, has, includes, find, first and last. These a ...

  9. asp and javascript: sql server export data to csv and to xls

    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <% //塗聚文 //20131021 functio ...

随机推荐

  1. linux查看前几条命令记录

    1.按上下箭头键2.history|more分页显示3.vi /etc/profile找HISTSIZE=1000,说明你最多能存1000条历史记录.4.!!执行最近执行的命令5.history|he ...

  2. echarts如何更改表格主题颜色

    vue项目中,需要使用echarts时,需要根据UI设计图进行图标颜色修改 方法一: 1.在script中引入echarts以及主题样式: import echarts from 'echarts'; ...

  3. WinServer-IIS-js无法加载问题

    IIS中无法加载JS文件错误 尝试下面的几种解决方法,一起用

  4. Spring中 @Autowired标签与 @Resource标签 的区别(转)

    spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解,如:@Resource. @PostConstruct及@PreDestroy. 1. @Autowi ...

  5. Swift的构造和析构过程

    构造过程 Swift的构造过程通过定义构造器来实现. 只是与Objective-C不同的是,Swift的构造器不须要返回值,相同也不须要表明Func. 另外值得提的是,当构造器中为存储型属性赋值时.不 ...

  6. 开心的小明(南阳oj49)(01背包)

    开心的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 小明今天非常开心.家里购置的新房就要领钥匙了,新房里有一间他自己专用的非常宽敞的房间.更让他高兴的是,妈妈 ...

  7. Activity嵌套多个Fragment实现横竖屏切换

    一.上图 二.需求 最近项目遇到个横竖屏切换的问题.较为复杂.在此记之. 1.Activity中竖屏嵌套3个Fragment,本文简称竖屏FP1,FP2,FP3. 2.当中竖屏FP1与FP2能够切换为 ...

  8. 终结者:负载均衡之Nginx(一)

            相信非常多人都听过Nginx.这个小巧的东西能够和Apache及IIS相媲美.那么它有什么作用呢?一句话.它是一个减轻Web应用server(如Tomcat)压力和实现Web应用ser ...

  9. Knockout源代码精析-怎样解析demo元素,获取到bindings(二)?

    接上文这里開始分析applyBindingsToNodeInternal.applyBindingsToNodeInternal方法例如以下: function applyBindingsToNode ...

  10. Linux常用命令之rpm安装命令

    转自:http://www.cnblogs.com/datasyman/p/6942557.html 在 Linux 操作系统下,几乎所有的软件均通过RPM 进行安装.卸载及管理等操作.RPM 的全称 ...