Redux + immutable.js 学习实践

  • 约199字
  • 技术
  • 2016年10月20日

用 react + redux 有一段时间了。之前都是自己写的 reducer,因为每次修改 state 时需要返回新的引用,所以需要特别注意,用起来总觉得不够方便。这两天学习了一下 Immutable.js , 准备把它引入到项目中和 redux 配合使用。先来设计 action 数据存取方法,最主要的几个方法如下:

// 根据 keyPath 设置 value,可以更新任意深位置的数据
// value 是简单值类型或者 Immutable 类型,也可以是函数 (oldValue) => newValue
dispatch(action.set(...keyPath, value));

// 删除 keyPath 对应的 value
dispatch(action.delete(...keyPath));

// 根据 keyPath 获取 value
state.getIn(keyPath);

Object 类型数据存取示例:

dispatch(action.set("page", "form", "name", "yanxi"));
dispatch(action.set("page", "form", "city", "beijing"));

state.getIn(["page", "form"]);
// 结果: Map {name: 'yanxi', city: 'beijing'}

state.getIn(["page", "form", "name"]);
state.getIn(["page", "form"]).get("name");
// 结果: yanxi

state.get("page").toJS();
// 结果: {form: {name: 'yanxi', city: 'beijing'}}

state.get("page").toObject();
// 结果: {form: Map {name: 'yanxi', city: 'beijing'}}

dispatch(action.delete("page", "form", "city"));

state.get("page").toObject();
// 结果: {form: Map {name: 'yanxi'}}

数组类型数据存取示例:

dispatch(action.set('page', 'persons', Immutable.fromJS([{name:'yanxi'}]))

state.getIn(['page', 'persons'])
// 结果: List [ Map { "name": "yanxi" } ]

state.getIn(['page', 'persons', 0, 'name'])
// 结果: yanxi

dispatch(action.set('page', 'persons', s => s.push(Map({name: 'zhangsan'})))

state.getIn(['page', 'persons', 1])
// 结果: Map { "name": "zhangsan" }

这么设计有两大好处,action 操作会非常方便:

  1. 可以直接根据 keyPath 更新任意深的数据
  2. 更新的时候如果最后一个参数指是函数,函数签名中第一个参数就是其更新前的值

注意事项:设置的 value 应该是一个值类型,即 Immutable 类型或者其他简单类型,而不能是一个 JS 对象或者数组。

主要实现部分如下:

actions

function set(...keyPath) {
  let value = keyPath.pop();
  return {
    type: "MAP_SET",
    keyPath,
    value,
  };
}

reducer

function (state = Map({}), action = {}) {
  if (action.type === 'MAP_SET') {
    if (typeof action.value === 'function') {
      return state.updateIn(action.keyPath, action.value);
    } else {
      return state.setIn(action.keyPath, action.value);
    }
  }

  return state;
}

相关文章

ETF(交易型指数基金)投资介绍

随着国内利率进一步降低,房价近几年也比较疲软,越来越多人会将资产配置到股市。对于没有专业股市投资的上班族来说,选择投资 ETF 基金是一个不错的选择。本文介绍了 ETF 的基础知识,并列举了A股的主要 ETF 品种。

查看更多

试用并感受 sina app engine

最近看到越来越多的网站和应用搭建在 SAE 上,这两天抽空看了下。今天下载了SAE的开发工具,写了个简单的hello world,又安装了这个WordPress博客系统,觉得还是挺有意思的。

查看更多

Standard JS 代码规范和提交代码时检查

公司的 JavaScript 项目最开始有一个代码规范,也用 jsHint 写了检查脚本。但是随着团队人员的逐渐增加和项目的时间紧迫,有一段时间没有特别强调要去做代码规范的检查。

查看更多