Questions tagged [immutable.js]

Immutable.js provides Persistent Immutable List, Stack, Map, OrderedMap, Set, OrderedSet and Record. They are highly efficient on modern JavaScript VMs (browser and nodejs) by using structural sharing via hash maps tries and vector tries as popularized by Clojure and Scala, minimizing the need to copy or cache data.

Immutable provides immutable data structures like List, Stack, Map, OrderedMap, Set and Record by using persistent hash maps tries and vector tries as popularized by Clojure and Scala. They achieve efficiency on modern JavaScript VMs by using structural sharing and minimizing the need to copy or cache data.

Immutable also provides a lazy Seq, allowing efficient chaining of collection methods like map and filter without creating intermediate representations. Create some Seq with Range and Repeat.

Getting started:

Install immutable using npm.

npm install immutable

Then require it into any module.

var Immutable = require('immutable');
var map = Immutable.Map({a:1, b:2, c:3});

Useful links:

License:

Immutable is BSD-licensed. We also provide an additional patent grant.

1206 questions
558
votes
4 answers

Index inside map() function

I am missing a option how to get the index number inside the map function using List from Immutable.js: var list2 = list1.map(mapper => { a: mapper.a, b: mapper.index??? }).toList(); Documentation shows that map() returns Iterable. Is…
Zygimantas
  • 8,547
  • 7
  • 42
  • 54
273
votes
13 answers

Why is immutability so important (or needed) in JavaScript?

I am currently working on React JS and React Native frameworks. On the half way road I came across Immutability or the Immutable-JS library, when I was reading about Facebook's Flux and Redux implementation. The question is, why is immutability so…
bozzmob
  • 12,364
  • 16
  • 50
  • 73
135
votes
7 answers

How to update element inside List with ImmutableJS?

Here is what official docs said updateIn(keyPath: Array, updater: (value: any) => any): List updateIn(keyPath: Array, notSetValue: any, updater: (value: any) => any): List updateIn(keyPath: Iterable, updater: (value: any)…
Vitalii Korsakov
  • 45,737
  • 20
  • 72
  • 90
61
votes
4 answers

How to set multiple fields at once in Immutable.js Record?

Looking at this, I think that Immutable. Record is the data structure for represent "javascript immutable objects", but I want to update several fields at once without creating several objects calling set each time. I want to do something like…
gabrielgiussi
  • 9,245
  • 7
  • 41
  • 71
56
votes
2 answers

How to update a value of a nested object in a reducer?

I have built my state like so const list = { categories: { Professional: { active: false, names: [ { id: 1, name: "Golf", active: false }, { id: 2, name: "Ultimate Frisbee", …
albert
  • 601
  • 1
  • 5
  • 6
51
votes
1 answer

How can I set a deeply nested value in Immutable.js?

When working with plain JavaScript objects it's easy to change a deeply nested object property: people.Thomas.nickname = "Mr. T"; But with Immutable I have to go through each property's ancestors before I have a new people object: var thomas =…
Matt Zeunert
  • 16,075
  • 6
  • 52
  • 78
51
votes
5 answers

How to use Immutable.js with redux?

Redux framework is using reducers to change app state in response to an action. The key requirement is that a reducer cannot modify an existing state object; it must produce a new object. Bad Example: import { ACTIVATE_LOCATION } from…
Gajus
  • 69,002
  • 70
  • 275
  • 438
47
votes
6 answers

How to check if object is Immutable?

Immutable object can be an instance of: Immutable.List Immutable.Map Immutable.OrderedMap Immutable.Set Immutable.OrderedSet Immutable.Stack
Gajus
  • 69,002
  • 70
  • 275
  • 438
45
votes
5 answers

When to use .toJS() with Immutable.js and Flux?

I'm trying to use ImmutableJS with my React / Flux application. My stores are Immutable.Map objects. I'm wondering at which point should I use .toJS() ? Should it be when the store's .get(id) returns ? or in the components with .get('member') ?
chollier
  • 912
  • 1
  • 7
  • 8
44
votes
2 answers

What is the difference between ImmutableJS Map() and fromJS()?

var a = {address: {postcode: 5085}} var b = Immutable.fromJS(a) var c = b.setIn(['address', 'suburb'], 'broadview').toJS(); // no error console.log(c); var d = Immutable.Map(a); var e = d.setIn(['address', 'suburb'], 'broadview').toJS(); // error…
sowdri
  • 2,193
  • 5
  • 23
  • 36
41
votes
1 answer

How to describe Immutable.js Map shape with Flow

I would like to describe the shape of a map using Immutable's flow type definitions. You can describe the shape of an object by: const stateShape: { id: number, isActive: boolean } = { id: 123, isActive: true }; Is there something similar…
William
  • 1,517
  • 1
  • 12
  • 26
40
votes
8 answers

How to specify null prop type in ReactJS?

I've got a prop on a ReactJS Component that's either null or an Immutable Map. At the bottom of my widget if I write: MyComponent.propTypes = { myMap: React.PropTypes.instanceOf(Immutable.Map) }; I am leaving this open to the possibility of…
user1261710
  • 2,539
  • 5
  • 41
  • 72
39
votes
6 answers

Why should I use immutablejs over object.freeze?

I have researched on net about the benefits of immutablejs over Object.freeze() but didn't find anything satisfying! My question is why I should use this library and work with non native data structures when I can freeze a plain old javascript…
alisabzevari
  • 8,008
  • 6
  • 43
  • 67
38
votes
2 answers

React-Redux complex (deep) state objects

Given my initial redux state is : const state = { currentView: 'ROOMS_VIEW', navbarLinks: List([ {name: 'Rooms', key: 'ROOMS_VIEW'}, {name: 'Dev', key: ''} ]), roomListsSelected: {group: 0, item: 0}, roomLists: [ { name:…
Steven Bayer
  • 1,847
  • 4
  • 15
  • 16
37
votes
3 answers

How to get union of several immutable.js Lists

So, I have List a: let a = Immutable.List([1]) and List b: let b = Immutable.List([2, 3]) I want to get List union === List([1, 2, 3]) from them. I try to merge them fist: let union = a.merge(b); // List([2, 3]) It seems like merge method…
Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79
1
2 3
80 81