@samitouri / QOS-React-2 / commits / 67cf95c16d

Avoid reading the property if hasOwnProperty is false

Easy with an Object.assign polyfill

Sebastian Markbage committed Feb 17, 2015 at 15:59 UTC 67cf95c16de981c527ed7b883a9ac8473dd8a1a8
2 files changed +28 -8
src/classic/element/ReactElementValidator.js
+15 -8
@@ -292,6 +292,19 @@ function warnForPropsMutation(propName, element) {
292 );
293 }
294
295 +// Inline Object.is polyfill
296 +function is(a, b) {
297 + if (a !== a) {
298 + // NaN
299 + return b !== b;
300 + }
301 + if (a === 0 && b === 0) {
302 + // +-0
303 + return 1 / a === 1 / b;
304 + }
305 + return a === b;
306 +}
307 +
308 /**
309 * Given an element, check if its props have been mutated since element
310 * creation (or the last call to this function). In particular, check if any
@@ -312,14 +325,8 @@ function checkAndWarnForMutatedProps(element) {
325
326 for (var propName in props) {
327 if (props.hasOwnProperty(propName)) {
315 - var valueChanged = originalProps[propName] !== props[propName];
316 - // Necessary because NaN !== NaN
317 - if (typeof originalProps[propName] === 'number' &&
318 - typeof props[propName] === 'number' &&
319 - isNaN(originalProps[propName]) && isNaN(props[propName])) {
320 - valueChanged = false;
321 - }
322 - if (!originalProps.hasOwnProperty(propName) || valueChanged) {
328 + if (!originalProps.hasOwnProperty(propName) ||
329 + !is(originalProps[propName], props[propName])) {
330 warnForPropsMutation(propName, element);
331
332 // Copy over the new value so that the two props objects match again
src/classic/element/__tests__/ReactElement-test.js
+13
@@ -348,4 +348,17 @@ describe('ReactElement', function() {
348 'Don\'t set .props.sound of the React component <Outer />.'
349 );
350 });
351 +
352 + it('does not warn for NaN props', function() {
353 + spyOn(console, 'warn');
354 + var Test = React.createClass({
355 + render: function() {
356 + return <div />;
357 + }
358 + });
359 + var test = ReactTestUtils.renderIntoDocument(<Test value={+undefined} />);
360 + expect(test.props.value).toBeNaN();
361 + expect(console.warn.argsForCall.length).toBe(0);
362 + });
363 +
364 });