There are two major reasons why you need to use seamless-immutable in your react application.
It's a bad practice to update props inside the component because it leads to much more problems than your thoughts(It will be a big topic but now it's known to all so I think I don't need to explain that, at least in this article). So, from the tech point of view, immutable is better.
To save the effort of differing props, the pure component does only compare the references of each property in props, called shallowly compares. If the references are the same, the re-render not happen. In using object or array we must very carefully to re-generate a new copy of them if the data changed. That's the reason why spread operator is introduced by redux and is highlighted.
seamless-immutable is a simple implementation of immutable. Less size of the bundle, less time you need to learn how to do mutation. And, the very great side is, you can visit the immutable data just like a primitive object/array. No more operators, grammar.
Get started
How to create the immutable copy of an object/array?
Immutable(object)
Immutable(array)
How to create the mutable copy(for easy modification without using replace/set/merge methods) of an immutable data?
Immutable.asMutable(object)
additional options:
Immutable.asMutable(object, { deep: true})
This will create a mutable copy for each property(or elements in an array). That means all properties will be new in the mutable copy(If deep = true then all nested property will be new generated as well). And that will leads to a performance issue in the application which has huge and complicated data because that react component will be re-render although it's not necessary.
The better idea is to only mutate the property when it's necessary. For example, there is an address list:
Like somebody said, every coin has two sides: Use 'asMutable' is easy for usual modifications like what you did before involving the concept `immutable', but it's bad for performance(for the big app). Try to update the object carefully with the operators provided by immutable is hard a lot to beginners, but it's good or required and much worth to do in a big app.
So let's learn how to update it as little as possible
They are merge, replace, set & setIn, update & updateIn, without.
Only set & setIn can be applied on both object and an array, others only work on an object.
merge, replace, set & setIn support additional parameters deep:true to perform a deep operation. Others don't.
ps. I will not introduced the method get and getIn since they are not about mutation.
merge
var obj = Immutable({status: "good", hypothesis: "plausible", errors: 0});
Immutable.merge(obj, {status: "funky", hypothesis: "confirmed"});
// returns Immutable({status: "funky", hypothesis: "confirmed", errors: 0})
var obj = Immutable({status: "bad", errors: 37});
Immutable.merge(obj, [
{status: "funky", errors: 1}, {status: "groovy", errors: 2}, {status: "sweet"}]);
// returns Immutable({status: "sweet", errors: 2})
// because passing an Array is shorthand for
// invoking a separate merge for each object in turn.
replace
var obj1 = Immutable({a: {b: 'test'}, c: 'test'});
var obj2 = Immutable.replace(obj1, {a: {b: 'test'}}, {deep: true});
// returns Immutable({a: {b: 'test'}});
obj1 === obj2
// returns false
obj1.a === obj2.a
// returns true because child .a objects were identical
asMutable is the easiest way to perform modification, but it harms performance sometimes.
All methods for mutating object/array returns a new immutable copy. If you have to perform more than one mutation on the data, please make sure every mutation works on the newer copy.
Why you need it?
There are two major reasons why you need to use seamless-immutable in your react application.
Read more in purecomponent
And why seamless-immutable?
seamless-immutable is a simple implementation of immutable. Less size of the bundle, less time you need to learn how to do mutation. And, the very great side is, you can visit the immutable data just like a primitive object/array. No more operators, grammar.
Get started
How to create the immutable copy of an object/array?
How to create the mutable copy(for easy modification without using replace/set/merge methods) of an immutable data?
additional options:
This will create a mutable copy for each property(or elements in an array). That means all properties will be new in the mutable copy(If deep = true then all nested property will be new generated as well). And that will leads to a performance issue in the application which has huge and complicated data because that react component will be re-render although it's not necessary.
The better idea is to only mutate the property when it's necessary. For example, there is an address list:
So let's learn how to update it as little as possible
They are merge, replace, set & setIn, update & updateIn, without.
ps. I will not introduced the method get and getIn since they are not about mutation.
merge
replace
set & setIn
Only set & setIn can be applied on an array.
When called with an Immutable Array, the property parameter is the index to be changed:
Like set, but accepts a nested path to the property.
update & updateIn
Returns an Immutable Object with a single property updated using the provided updater function.
All additional arguments will be passed to the updater function.
updateIn like setIn that it accetps a nested path to be property.
without
Returns an Immutable Object excluding the given keys or keys/values satisfying the given predicate from the existing object.
Multiple keys can be provided, either in an Array or as extra arguments.
Summary of seamless-immutable
Thanks for your reading.