Skip to content

renderComponent without jsx: clone the props object passed in, to avoid pollution #570

@tommyh

Description

@tommyh

This issue only manifests when you are using vanilla-js (not-jsx) when making a React.renderComponent call.

Issue: When you pass in an object to renderComponent, that object is mutated with the props. So if you try to use that object again to render a different component, the props from the first component will pollute the props in the second component.

Example code:

var Hello = React.createClass({
    getDefaultProps: function(){
        return {
            randomNumber: Math.random()
        };
    },
    render: function() {
        return <div>{this.props.name}: {this.props.randomNumber}</div>;
    }
});

var configData = {
    foo: "bar"
};

configData.name = "Example1";
React.renderComponent(Hello(configData), document.getElementById("example1"));

configData.name = "Example2";
React.renderComponent(Hello (configData), document.getElementById("example2"));

The this.props.randomNumber from the first component will get carried to the second component because configData will be mutated.

Example of this code: http://jsfiddle.net/ZeNLA/3/

I asked this in reactjs irc, and here was the response:

[16:01] <@zpao> tommyh: it's a good question. one of the ways we keep React fast is to avoid clones when possible
[16:02] <@zpao> cloning here will be really expensive since this is such a common task. BUT it's not outrageous...
[16:05] <tommyh> yeah.  i figure most people use jsx so it's not an issue, but as its not a "mandatory dependency" it would be a bit of a narly issue to get stung by
[16:05] <tommyh> and i can definetly code around it, just thought i'd get your guys thoughts on it
[16:06] <@zpao> thanks for bringing it up... if you could file an issue too where we could discuss this with other people too, that would be helpdful

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