Skip to content

Conversation

@sophiebits
Copy link
Collaborator

Depends on #575; fixes #570. Now we'll be in trouble if someone tries to share objects between calls to getDefaultProps but that was already true of getInitialState and I haven't heard any complaints there.

This is the same number of allocations as before; we're just copying props in the other direction. (In any case, the copy happens only on mount and there are a couple dozen costlier things we're doing already at that time.)

Depends on facebook#575; fixes facebook#570.  Now we'll be in trouble if someone tries to share objects between calls to getDefaultProps but that was already true of getInitialState and I haven't heard any complaints there.

This is the same number of allocations as before; we're just copying props in the other direction. (In any case, the copy happens only on mount and there are a couple dozen costlier things we're doing already at that time.)
@tommyh
Copy link

tommyh commented Nov 21, 2013

+1.

Can you explain what you mean by "we'll be in trouble if someone tries to share objects between calls to getDefaultProps but that was already true of getInitialState"? Like modifying a global object in the getDefaultProps or getInitialState? A few lines of pseudo code would help me understand this point. :)

@sophiebits
Copy link
Collaborator Author

Yep, if you do

var defaults = {banana: 17};
var Component = React.createClass({
    getDefaultProps: function() { return defaults; },
    // ...
});

then all components would share a .props object. Seems unlikely though.

@petehunt
Copy link
Contributor

petehunt commented Dec 1, 2013

pulling this in

petehunt added a commit that referenced this pull request Dec 2, 2013
@petehunt petehunt merged commit 92b62bf into facebook:master Dec 2, 2013
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to clone this._defaultProps here? this mutates the default prop object which gets reused on each pass.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my original PR comment: "Now we'll be in trouble if someone tries to share objects between calls to getDefaultProps but that was already true of getInitialState and I haven't heard any complaints there."

But we can copy if that's a problem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MyComponent = React.createClass({
  getDefaultProps: function() { return {}; },
  render: function() { ... }
};

// render <MyComponent disabled={true} />
// update with <MyComponent />

we'll see that disabled is true for the second pass because this._defaultProps has been modified.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, you're right.

zpao pushed a commit that referenced this pull request Dec 6, 2013
This reverts #576

This approach mutates the default props for the instance on each update,
which causes incorrect behavior. discussed with @balpert. can look into
cloning but this unbreaks.
toptaldev92 pushed a commit to toptaldev92/react_project that referenced this pull request Jul 28, 2021
This reverts facebook/react#576

This approach mutates the default props for the instance on each update,
which causes incorrect behavior. discussed with @balpert. can look into
cloning but this unbreaks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants