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. systemctl 控制单元

    [root@web01 ~]# systemctl status sshd.service ● sshd.service - OpenSSH server daemon Loaded: loaded ...

  2. done

  3. php 文件加载方式

    两种加载文件的方式 include require 使用场景: 动态加载文件的时候,使用include,否则使用require. 示例: # 引入php文件--include方式 inlcude(&q ...

  4. shell 测试命令

    一.使用 test 命令可以对文件.字符串等进行测试,一般配合控制语句使用. 1.字符串测试 test str1 = str2 //测试字符串是否相等 test str1 != str2 //测试字符 ...

  5. 循环语句第3种 FOR ... in ... LOOP END LOOP;

    --------第3种--------  FOR ... in ... LOOP  END LOOP;    BEGIN    FOR i IN 1..10 LOOP      dbms_output ...

  6. JAVA深克隆与浅克隆1

    复制就是得到一个副本 克隆就是复制一个对象的复本.但一个对象中可能有基本数据类型,如:int,long,float    等,也同时含有非基本数据类型如(数组,集合等)被克隆得到的对象基本类型的值修改 ...

  7. [using_microsoft_infopath_2010]Chapter2 表单需求,使用表决矩阵

    本章概要 1.从模板创建表单 2.从创建表单收集需求 3.使用全部表单决策 4.决定需要创建哪种表单

  8. [Hyperapp] Interact with the State Object through Hyperapp Action functions

    Hyperapp is an ultra lightweight (1kb), minimal, functional, JavaScript library for building UIs. It ...

  9. hdu Swipe Bo(bfs+状态压缩)错了多次的题

    Swipe Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  10. 调用支付宝SDK问题

    近期做了一个项目里面要有支付.银联.支付宝,微信支付 我先一个一个写吧 先说支付宝SDK 支付宝SDK放进project里面之后肯定会报错.这时候你就要一个一个改掉 1. 2. 3. 哎 我懒得写了. ...