Skip to content

Commit

Permalink
Merge pull request facebook#3175 from sebmarkbage/fixhasownorder
Browse files Browse the repository at this point in the history
Avoid reading the property if hasOwnProperty is false
  • Loading branch information
sebmarkbage committed Feb 18, 2015
2 parents de3ecab + 67cf95c commit c612075
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/classic/element/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ function warnForPropsMutation(propName, element) {
);
}

// Inline Object.is polyfill
function is(a, b) {
if (a !== a) {
// NaN
return b !== b;
}
if (a === 0 && b === 0) {
// +-0
return 1 / a === 1 / b;
}
return a === b;
}

/**
* Given an element, check if its props have been mutated since element
* creation (or the last call to this function). In particular, check if any
Expand All @@ -312,14 +325,8 @@ function checkAndWarnForMutatedProps(element) {

for (var propName in props) {
if (props.hasOwnProperty(propName)) {
var valueChanged = originalProps[propName] !== props[propName];
// Necessary because NaN !== NaN
if (typeof originalProps[propName] === 'number' &&
typeof props[propName] === 'number' &&
isNaN(originalProps[propName]) && isNaN(props[propName])) {
valueChanged = false;
}
if (!originalProps.hasOwnProperty(propName) || valueChanged) {
if (!originalProps.hasOwnProperty(propName) ||
!is(originalProps[propName], props[propName])) {
warnForPropsMutation(propName, element);

// Copy over the new value so that the two props objects match again
Expand Down
13 changes: 13 additions & 0 deletions src/classic/element/__tests__/ReactElement-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,17 @@ describe('ReactElement', function() {
'Don\'t set .props.sound of the React component <Outer />.'
);
});

it('does not warn for NaN props', function() {
spyOn(console, 'warn');
var Test = React.createClass({
render: function() {
return <div />;
}
});
var test = ReactTestUtils.renderIntoDocument(<Test value={+undefined} />);
expect(test.props.value).toBeNaN();
expect(console.warn.argsForCall.length).toBe(0);
});

});

0 comments on commit c612075

Please sign in to comment.