Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

immutable.js

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 this

class LoginContext extends Immutable.Record(
{ logged : false, loading: false, error: false, user: null}){
}

var loginContext = new LoginContext()

var anotherContext = loginContext.set({'logged':'true', 'error':'false'})

I read that you can't pass an object to Record.set() for API consistency:

Consistency with other uses of set both in this library and in ES6. Map and Set's set can't accept an object, because their keys can be anything, not just strings. Records must have strings, but keeping the API consistent was important.

And I know that I could use:

var anotherContext = loginContext.withMutations(function (record) {  
  record.set('logged','true').set('error','true'); 
});

There is another way or I'm misusing Record?

like image 435
gabrielgiussi Avatar asked Jul 27 '15 23:07

gabrielgiussi


People also ask

Are sets immutable in Javascript?

Iteration order of a Set is undefined, however is stable. Multiple iterations of the same Set will iterate in the same order. Set values, like Map keys, may be of any type. Equality is determined using Immutable.is , enabling Sets to uniquely include other Immutable collections, custom value types, and NaN.

What is Immutablejs?

Immutable. js is a library that supports an immutable data structure. It means that once created data cannot be changed. It makes maintaining immutable data structures easier and more efficient. The tool supports data structure like: List, Map, Set and also structures that are not implemented in .

Are typescript records immutable?

React apps with Redux are built around an immutable state model. Your entire application state is one immutable data structure stored in a single variable.

How immutable js works?

Working with immutable objects A pure function has two properties that make it unique: The value it returns is dependent on the input passed. The returned value will not change as long as the inputs do not change. It does not change things outside of its scope.


4 Answers

Personally I prefer the merge syntax, it feels more natural:

const newContent = oldContext.merge({
    "logged": true, 
    "error": false
});

If you were doing something very complex, maybe the transient version would be better, but I just can't imagine where.

It also means you can leverage mergeDeep if needed.

like image 192
Chris Avatar answered Oct 31 '22 22:10

Chris


To answer your original question we will use .withMutations()

Here an excerpt from the docs:

Applying a mutation to create a new immutable object results in some overhead, which can add up to a minor performance penalty. If you need to apply a series of mutations locally before returning, Immutable gives you the ability to create a temporary mutable (transient) copy of a collection and apply a batch of mutations in a performant manner by using withMutations. In fact, this is exactly how Immutable applies complex mutations itself.

So you can write something along the lines:

loginContext.withMutations((ctx) => {
    ctx.set('logged', true).set('error', false)
});

Besides that I thought that Records could also be used with ordinary js dot notation.

Good luck!

like image 31
RemEmber Avatar answered Oct 31 '22 23:10

RemEmber


Why don't you just chain multiple sets? Like so:

ImmutableObj.set('key', 'value')
.set('key2', 'value2')
.set('key3', 'value3'); 
like image 19
James111 Avatar answered Oct 31 '22 21:10

James111


You should do a few things differently. First, don't extend Immutable.Record but use the constructor:

var LoginContext = Immutable.Record({
  logged:false,
  loading:false,
  user: null  
}, 'LoginContext');

This returns a class that you can instantiate with an object:

var anotherContext = new LoginContext({logged:true, error:false});
like image 1
Matthias Winkelmann Avatar answered Oct 31 '22 21:10

Matthias Winkelmann