Skip to content

React.addons.update should return the input object when we replace a value by itself #1923

@slorber

Description

@slorber

This code shows the problem

var root = {
    prop: "propValue",
    nested: {
        nestedProp: "nestedPropValue"
    }
}

var rootCopy = React.addons.update(root,{
    nested: { $set: root.nested }
});

var rootCopy2 = React.addons.update(root,{
    nested: { nestedProp : {$set: root.nested} }
});

console.debug("root === rootCopy",root === rootCopy);
console.debug("root.prop === rootCopy.prop",root.prop === rootCopy.prop);
console.debug("root.nested === rootCopy.nested",root.nested === rootCopy.nested);

console.debug("root === rootCopy2",root === rootCopy);
console.debug("root.prop === rootCopy2.prop",root.prop === rootCopy2.prop);
console.debug("root.nested === rootCopy2.nested",root.nested === rootCopy2.nested);
root === rootCopy false app.js:46
root.prop === rootCopy.prop true app.js:47
root.nested === rootCopy.nested true app.js:48

root === rootCopy2 false app.js:50
root.prop === rootCopy2.prop true app.js:51
root.nested === rootCopy2.nested false 

I woould expect all these equality to be true.

I think this is the point of immutability/structural sharing to not create useless objects when their inner properties are equally the same.

var object = {}
object = React.addons.update(object, { prop: { $set: propValue} });
object = React.addons.update(object, { prop: { $set: propValue} });

On the 2nd call, it makes sense that the object returned is the object from the arguments.

Ensuring this can help to avoid unexpected behavior in shouldComponentUpdate and prevent some useless renderings that I have encountered (when user clicks twice on the same menu element for exemple)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions