Avoid reading the property if hasOwnProperty is false

Easy with an Object.assign polyfill
This commit is contained in:
Sebastian Markbage 2015-02-17 15:59:01 -08:00
parent de3ecabd6c
commit 67cf95c16d
2 changed files with 28 additions and 8 deletions

View File

@ -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
@ -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

View File

@ -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);
});
});