User Tools

Site Tools


Table of Contents


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