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

In this example, var a = {address: {postcode: 5085}} var d = Immutable.Map(a); Here, d.get(‘address’) is immutable. It’s value cannot change to any other objects. We can only create a new Object from the existing object using the Immutable.Map.set() function of ImmutableJS. But, the object referenced by d.get(‘address’) i.e, {postcode:5085} is a standard JavaScript object. … Read more

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

Ideally, never! If your Flux stores are using Immutable.js then try to maintain all the way through. Use React.addons.ReactComponentWithPureRenderMixin to achieve a memoization performance win (it adds a shouldComponentUpdate methods). When rendering, you may need to call toJS() as React v0.12.x only accepts Array as children: render: function () { return ( <div> {this.props.myImmutable.map(function (item) … Read more

How to use Immutable.js with redux?

Taking your good example with Immutable import { ACTIVATE_LOCATION } from ‘./actions’; import { Map } from ‘immutable’; const initialState = Map({}) export let ui = (state = initialState, action) => { switch (action.type) { case ACTIVATE_LOCATION: return state.set(‘activeLocationId’, action.id); default: return state; } };

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

This is very common thing, and, actually, quite daunting. As far as I know, there is no really good and well-adopted solution in plain JS. Initially, Object.assign approach was used: return Object.assign({}, state, { categories: Object.assign({}, state.categories, { Professional: Object.assign({}, state.Professional, { active: true }) }) }); This is too straightforward and cumbersome, I admit … Read more

How to update element inside List with ImmutableJS?

The most appropriate case is to use both findIndex and update methods. list = list.update( list.findIndex(function(item) { return item.get(“name”) === “third”; }), function(item) { return item.set(“count”, 4); } ); P.S. It’s not always possible to use Maps. E.g. if names are not unique and I want to update all items with the same names.

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

I have recently been researching the same topic. I’ll do my best to answer your question(s) and try to share what I have learned so far. The question is, why is immutability so important? What is wrong in mutating objects? Doesn’t it make things simple? Basically it comes down to the fact that immutability increases … Read more

Index inside map() function

You will be able to get the current iteration’s index for the map method through its 2nd parameter. Example: const list = [ ‘h’, ‘e’, ‘l’, ‘l’, ‘o’]; list.map((currElement, index) => { console.log(“The current iteration is: ” + index); console.log(“The current element is: ” + currElement); console.log(“\n”); return currElement; //equivalent to list[index] }); Output: The … Read more