Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5929. Resolve to default props when config key is set to undefined in cloneElement #5997

Merged
merged 1 commit into from
Feb 19, 2016

Conversation

truongduy134
Copy link
Contributor

Fix #5929. Resolve to default props when config key is set to undefined in cloneElement

var Foo = React.createClass({
   getDefaultProps: function() {
      return {foo: 'bar'};
   },
   render: function() {
      return React.createElement('span', null, this.props.prop);
   },
});

clonedElement = React.cloneElement(React.createElement(Foo, {foo: 'foo'}), {foo: undefined});
console.log(element.props.foo); // Should be 'bar' instead of undefined

@facebook-github-bot
Copy link

@truongduy134 updated the pull request.

@@ -245,7 +245,8 @@ ReactElement.cloneElement = function(element, config, children) {
// Remaining properties override existing props
for (propName in config) {
if (config.hasOwnProperty(propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)) {
!RESERVED_PROPS.hasOwnProperty(propName) &&
config[propName] !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is wrong, because setting the value to undefined SHOULD change the element if the prop was set to a non-default value when the element was created. The desired behavior would be to use the prop's default value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I see. Will update the behavior and test cases. Thanks @jimfb

@truongduy134 truongduy134 changed the title Fix #5929. Do not override props with undefined in cloneElement Fix #5929. Resolve to default props when config key is set to undefined in cloneElement Feb 8, 2016
@facebook-github-bot
Copy link

@truongduy134 updated the pull request.

// Resolve default props
if (element.type && element.type.defaultProps) {
var defaultProps = element.type.defaultProps;
for (propName in defaultProps) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably don't need to introduce a loop here, since we know the only way this could happen is if the config prop is undefined. We can just add an undefined check to the existing loop above.

@truongduy134
Copy link
Contributor Author

@jimfb , @zpao: Thank you for the reviews. Addressed your comments

@facebook-github-bot
Copy link

@truongduy134 updated the pull request.

for (propName in config) {
if (config.hasOwnProperty(propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)) {
props[propName] = config[propName];
if (config[propName] === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

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

&& defaultProps !== undefined

In cloneElement, when key in input config is set to undefined, the associated
prop value should be resolved to default prop values
@facebook-github-bot
Copy link

@truongduy134 updated the pull request.

@sebmarkbage
Copy link
Collaborator

This is a breaking change. We might need a warning upgrade path. We should investigate first to know how likely this is to break and how bad it could be.

@truongduy134
Copy link
Contributor Author

@jimfb : Do we need to add a warning message ?

@jimfb
Copy link
Contributor

jimfb commented Feb 10, 2016

@truongduy134 I'll need to do some checks internally and report back. It will take at least a few days.

@truongduy134
Copy link
Contributor Author

@jimfb : Do we have any update on this PR?

@jimfb
Copy link
Contributor

jimfb commented Feb 19, 2016

Yep, we were just looking at the results, it looks like the impact is low enough that we are comfortable with it. Will require one minor change internally when we sync.

@jimfb jimfb closed this Feb 19, 2016
@jimfb jimfb reopened this Feb 19, 2016
jimfb added a commit that referenced this pull request Feb 19, 2016
Fix #5929. Resolve to default props when config key is set to undefined in cloneElement
@jimfb jimfb merged commit 70b5eda into facebook:master Feb 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants