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. jquery @keyframes 动态添加

    需要写一个css3的动画效果,且需要按着写的事件同事进行需控制样式 css代码 @keyframes spin1 { 0% { transform: rotate(225deg); } 50% { t ...

  2. 题解 P1337 【[JSOI2004]平衡点 / 吊打XXX】

    这道题调了好久,果然非洲人是得不到眷顾的吗... 本题采用模拟退火解决. 模拟退火是一种简洁明了而又高效的近似算法,基本上可以套到任何求最优解的题目上去. 它的原理是模拟物理中金属退火的现象,凭借选手 ...

  3. js-DOM操作基本知识

  4. Docker学习总结(13)——从零开始搭建Jenkins+Docker自动化集成环境

    本文只简单标记下大概的步骤,具体搭建各个部分的细节,还请自行搜索.第一.二部分只是对Jenkins和Docker的简单介绍,熟悉的同学请直接跳到第三部分. 一.关于Jenkins Jenkins简介 ...

  5. ASP.NET-常用正则表达式

    常用正则表达式 正则: [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}", ErrorMes ...

  6. Tarjan强联通分量【模板】

    #include <algorithm> #include <cstdio> using namespace std; ); int n,m,v,u; int edgesum, ...

  7. jqGrid添加删除功能(不和数据库交互)

    jqGrid添加删除功能(不和数据库交互) 一.背景需求 项目中需要在前端页面动态的添加行,删除行,上下移动行等,同时还不和数据库交互.一直在用jqGrid展示表格的我们,从没有深入的研究过它,当然看 ...

  8. IEEE的论文需要注意的一些

    详细看最近的IEEE会议模板的时候,忽然注意到表的注序号应该用字母:另外,引用未发表的论文,如果是archive上的要使用archive规定的方法,或者标注``unpublished''

  9. Eclipse中將Java项目转变为Java Web项目

    1.在项目上点击右键=>properties,在Project Facets配置项中,勾选Dynamic Web Module.Java.JavaScript选项. 2.用记事本打开项目目录下的 ...

  10. sharding-jdbc,轻量级数据库分库分表中间件

    Sharding-JDBC是当当应用框架ddframe中,从关系型数据库模块dd-rdb中分离出来的数据库水平分片框架,实现透明化数据库分库分表访问.Sharding-JDBC是继dubbox和ela ...