User Tools

Site Tools


immutable.js_lists

Table of Contents

From http://untangled.io/immutable-js-an-introduction-with-examples-written-for-humans/

Immutable Lists

Immutable objects create new objects when anything is done to them.

Adding new elements to a List

import Immutable, { Map, List } from 'immutable';

// Immutable.js non-mutating List.push
const collection = Immutable.List.of('ironMan'); //create new Immutable List
const newCollection = collection.push('captainAmerica'); //add new element and save to new variable

// Output:
alert(collection);  //gives List["ironMan"]
alert(newCollection); //gives List [ "ironMan", "captainAmerica" ]

We're not used to data with unchanging values

How do you deal with it?

1. Think of Immutable data as a value, or constant.

7 is 7, 8 is 8, List[“ironMan”] is List[“ironMan”], List [ “ironMan”, “captainAmerica” ] is List [ “ironMan”, “captainAmerica” ]

2. Think of it as the state, or version, of the data.

Donald Trump is the current president of the USA.

Abraham Lincoln is the first president of the USA.

These are facts, and will never change.

















immutable.js_lists.txt · Last modified: 2018/09/10 22:47 by 119.94.96.186