[Immutable.js] Updating nested values with ImmutableJS

The key to being productive with Immutable JS is understanding how to update values that are nested. Using setIn you can place a new value directly into an existing or new path. If you need access to the previous value before setting the new one, you can use updateIn instead. updateIn accepts the same path lookups as setIn, but gives you a callback function instead so that you can use the previous value however you wish and return an updated version.

const {Map, List, fromJS} = Immutable;

const state = fromJS({
    home: {
        loading: false,
        messages: [
            {
                type: ‘info‘,
                message: ‘Welcome to our website‘
            }
        ]
    }
});

// Add a new message with updateIn
const updated = state.updateIn([‘home‘, ‘messages‘], msgs => {
    return msgs.push(Map({type: ‘info‘, message: ‘Hi there!‘}));
});
console.log(updated.getIn([‘home‘, ‘messages‘]).toJS());

// Update a message in a known path
const updated2 = state.setIn([‘home‘, ‘messages‘, 0, ‘message‘], ‘new message!‘);
console.log(updated2.getIn([‘home‘, ‘messages‘]).toJS());
时间: 2024-10-06 20:09:57

[Immutable.js] Updating nested values with ImmutableJS的相关文章

React+Immutable.js的心路历程

这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以被改变的数据.可以通过使用Immutable Data,可以让我们更容易的去处理缓存.回退.数据变化检测等问题,简化我们的开发. 我们知道 react.js很著名的就是它处理dom的做法,它是通过Virtual Dom来查看diff,然后再改动需要改动的Dom.但是有个问题当state更新时,如果数

[Immutable.js] Differences between the Immutable.js Map() and List()

The Immutable.js Map() is analogous to a Javascript Object or Hash since it is comprised of key-value pairs. The Immutable.js List() is analogous to a Javascript Array and contains many of the same native methods. Let's compare the two and dive into

[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.js class contains a prefixed "to" method like Map.toList(), Map.toSet(), etc. Converting these types sometimes results in a loss of data, as we

Immutable.js使用教程记录

官方文档: http://facebook.github.io/immutable-js/docs/#/ github: https://github.com/facebook/immutable-js/ 述immutable.js的用法: http://www.cnblogs.com/yoissee/p/6007251.html Immutable注意点: http://blog.csdn.net/liu__hua/article/details/52974152

Immutable.js尝试(node.js勿入)

最近做一些复杂html常常需要在页面做一些数据处理,常常在想如果 js有list 这种数据结构多少,今天逛github时 发现有Immutable.js 这个项目https://github.com/facebook/immutable-js/ 文档地址:http://facebook.github.io/immutable-js/docs/#/ 在浏览器尝试了下发现还是不错的,起码不用自己用js写list的实现. 1 var list=Immutable.List().asMutable();

[Immutable.js] Exploring Sequences and Range() in Immutable.js

Understanding Immutable.js's Map() and List() structures will likely take you as far as you want to go with immutable programming. They have only small semantic differences between each other and the remaining structures in the Immutable.js family. S

大话immutable.js

为啥要用immutable.js呢.毫不夸张的说.有了immutable.js(当然也有其他实现库)..才能将react的性能发挥到极致!要是各位看官用过一段时间的react,而没有用immutable那么本文非常适合你. 1,对于react的来说,如果父组建有多个子组建 想象一下这种场景,一个父组建下面一大堆子组建.然后呢,这个父组建re-render.是不是下面的子组建都得跟着re-render.可是很多子组建里面是冤枉的啊!!很多子组建的props 和 state 然而并没有改变啊!!虽然

[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. Objects are converted into maps. Arrays are converted into lists. The fromJS() method can also take a reviver function for custom conversions.  Object to Im

[Immutable.js] Working with Subsets of an Immutable.js Map()

Immutable.js offers methods to break immutable structures into subsets much like Array--for instance it has the all powerful slice()--and unlike Array it offers functional methods like take() and skip(). You get the best of both the imperative and fu