• Immutability Helpers
    • The main idea
    • Available commands
    • Examples
      • Simple push
      • Nested collections
      • Updating a value based on its current one
      • (Shallow) merge

    Immutability Helpers

    React lets you use whatever style of data management you want, including mutation. However, if you can use immutable data in performance-critical parts of your application it’s easy to implement a fast shouldComponentUpdate() method to significantly speed up your app.

    Dealing with immutable data in JavaScript is more difficult than in languages designed for it, like Clojure. However, we’ve provided a simple immutability helper, update(), that makes dealing with this type of data much easier, without fundamentally changing how your data is represented. You can also take a look at Facebook’s Immutable-js and the Advanced Performance section for more detail on Immutable-js.

    The main idea

    If you mutate data like this:

    1. myData.x.y.z = 7;
    2. // or...
    3. myData.a.b.push(9);

    You have no way of determining which data has changed since the previous copy has been overwritten. Instead, you need to create a new copy of myData and change only the parts of it that need to be changed. Then you can compare the old copy of myData with the new one in shouldComponentUpdate() using triple-equals:

    1. var newData = deepCopy(myData);
    2. newData.x.y.z = 7;
    3. newData.a.b.push(9);

    Unfortunately, deep copies are expensive, and sometimes impossible. You can alleviate this by only copying objects that need to be changed and by reusing the objects that haven’t changed. Unfortunately, in today’s JavaScript this can be cumbersome:

    1. var newData = extend(myData, {
    2. x: extend(myData.x, {
    3. y: extend(myData.x.y, {z: 7}),
    4. }),
    5. a: extend(myData.a, {b: myData.a.b.concat(9)})
    6. });

    While this is fairly performant (since it only makes a shallow copy of log n objects and reuses the rest), it’s a big pain to write. Look at all the repetition! This is not only annoying, but also provides a large surface area for bugs.

    update() provides simple syntactic sugar around this pattern to make writing this code easier. This code becomes:

    1. var update = require('react-addons-update');
    2. var newData = update(myData, {
    3. x: {y: {z: {$set: 7}}},
    4. a: {b: {$push: [9]}}
    5. });

    While the syntax takes a little getting used to (though it’s inspired by MongoDB’s query language) there’s no redundancy, it’s statically analyzable and it’s not much more typing than the mutative version.

    The $-prefixed keys are called commands. The data structure they are “mutating” is called the target.

    Available commands

    • {$push: array} push() all the items in array on the target.
    • {$unshift: array} unshift() all the items in array on the target.
    • {$splice: array of arrays} for each item in arrays call splice() on the target with the parameters provided by the item.
    • {$set: any} replace the target entirely.
    • {$merge: object} merge the keys of object with the target.
    • {$apply: function} passes in the current value to the function and updates it with the new returned value.

    Examples

    Simple push

    1. var initialArray = [1, 2, 3];
    2. var newArray = update(initialArray, {$push: [4]}); // => [1, 2, 3, 4]

    initialArray is still [1, 2, 3].

    Nested collections

    1. var collection = [1, 2, {a: [12, 17, 15]}];
    2. var newCollection = update(collection, {2: {a: {$splice: [[1, 1, 13, 14]]}}});
    3. // => [1, 2, {a: [12, 13, 14, 15]}]

    This accesses collection‘s index 2, key a, and does a splice of one item starting from index 1 (to remove 17) while inserting 13 and 14.

    Updating a value based on its current one

    1. var obj = {a: 5, b: 3};
    2. var newObj = update(obj, {b: {$apply: function(x) {return x * 2;}}});
    3. // => {a: 5, b: 6}
    4. // This is equivalent, but gets verbose for deeply nested collections:
    5. var newObj2 = update(obj, {b: {$set: obj.b * 2}});

    (Shallow) merge

    1. var obj = {a: 5, b: 3};
    2. var newObj = update(obj, {$merge: {b: 6, c: 7}}); // => {a: 5, b: 6, c: 7}