"merge" and "setIn" in seamless-immutable

seamless-immutable

Immutable JS data structures which are backwards-compatible with normal Arrays and Objects.

Use them in for loops, pass them to functions expecting vanilla JavaScript data structures, etc.

  1. create seamless immutable data
Immutable.from([1, 2, 3]);
// is functionally the same as calling:
Immutable([1, 2, 3])
  1. setIn

Like set, but accepts a nested path to the property.

var obj = Immutable({type: {main: "parrot", sub: "Norwegian Blue"}, status: "alive"});
Immutable.setIn(obj, ["type", "sub"], "Norwegian Ridgeback");
// returns Immutable({type: {main: "parrot", sub: "Norwegian Ridgeback"}, status: "alive"})
  1. merge
var obj = Immutable({status: "good", hypothesis: "plausible", errors: 0});
Immutable.merge(obj, {status: "funky", hypothesis: "confirmed"});
// returns Immutable({status: "funky", hypothesis: "confirmed", errors: 0})

Returns an Immutable Object containing the properties and values of both this object and the provided object, prioritizing the provided object’s values whenever the same key is present in both objects.

var obj = Immutable({status: "bad", errors: 37});
Immutable.merge(obj, [
  {status: "funky", errors: 1}, {status: "groovy", errors: 2}, {status: "sweet"}]);
// returns Immutable({status: "sweet", errors: 2})
// because passing an Array is shorthand for
// invoking a separate merge for each object in turn.

Multiple objects can be provided in an Array in which case more merge invocations will be performed using each provided object in turn.

A third argument can be provided to perform a deep merge: {deep: true}

attention
> var Immutable = require("seamless-immutable").static;
> var obj = Immutable({fundDetail:{isLoading:true,hasError:true}})
{ fundDetail: { isLoading: true, hasError: true } }
> Immutable.merge(obj,{fundDetail:{content:{content1:'something contents'}}})
{ fundDetail: { content: { content1: 'something contents' } } }
//in this case, content:{...} will replace all parallel items(eg. isLoading, hasError) in fundDetail

So, we need to use setIn

> Immutable.setIn(obj,['fundDetail','content'],{content1:'something contents'})
{ fundDetail: 
   { isLoading: true,
     hasError: true,
     content: { content1: 'something contents' } } }
How to write in react-native?
const obj = Immutable.from({......})
obj.setIn(['fundDetail', 'content'], filteredContent)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值