Immutable JS data structures which are backwards-compatible with normal Arrays and Objects.
Use them in for loops, pass them to functions expecting vanilla JavaScript data structures, etc.
- create seamless immutable data
Immutable.from([1, 2, 3]);
// is functionally the same as calling:
Immutable([1, 2, 3])
- setIn
Like set, but accepts a nested path to the property.
var obj = Immutable({type: {main: "parrot", sub: "Norwegian Blue"}, status: "alive"});
Immutable.setIn(obj, ["type", "sub"], "Norwegian Ridgeback");
// returns Immutable({type: {main: "parrot", sub: "Norwegian Ridgeback"}, status: "alive"})
- 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})
Returns an Immutable Object containing the properties and values of both this object and the provided object, prioritizing the provided object’s values whenever the same key is present in both objects.
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.
Multiple objects can be provided in an Array in which case more merge invocations will be performed using each provided object in turn.
A third argument can be provided to perform a deep merge: {deep: true}
attention
> var Immutable = require("seamless-immutable").static;
> var obj = Immutable({fundDetail:{isLoading:true,hasError:true}})
{ fundDetail: { isLoading: true, hasError: true } }
> Immutable.merge(obj,{fundDetail:{content:{content1:'something contents'}}})
{ fundDetail: { content: { content1: 'something contents' } } }
//in this case, content:{...} will replace all parallel items(eg. isLoading, hasError) in fundDetail
So, we need to use setIn
> Immutable.setIn(obj,['fundDetail','content'],{content1:'something contents'})
{ fundDetail:
{ isLoading: true,
hasError: true,
content: { content1: 'something contents' } } }
How to write in react-native?
const obj = Immutable.from({......})
obj.setIn(['fundDetail', 'content'], filteredContent)