diff --git a/src/context.js b/src/context.js index 65a06e38a..0c532c6e1 100644 --- a/src/context.js +++ b/src/context.js @@ -4,7 +4,8 @@ let defaultOptions = { bindI18n: 'languageChanged loaded', bindStore: 'added removed', translateFuncName: 't', - nsMode: 'default' + nsMode: 'default', + usePureComponent: false }; let i18n; diff --git a/src/shallowEqual.js b/src/shallowEqual.js new file mode 100644 index 000000000..591ee0bd4 --- /dev/null +++ b/src/shallowEqual.js @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @providesModule shallowEqual + * @typechecks + * @flow + */ + +/* eslint-disable no-self-compare */ + +const hasOwnProperty = Object.prototype.hasOwnProperty; + +/** + * inlined Object.is polyfill to avoid requiring consumers ship their own + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is + */ +function is(x, y) { + // SameValue algorithm + if (x === y) { + // Steps 1-5, 7-10 + // Steps 6.b-6.e: +0 != -0 + // Added the nonzero y check to make Flow happy, but it is redundant + return x !== 0 || y !== 0 || 1 / x === 1 / y; + } + // Step 6.a: NaN == NaN + return x !== x && y !== y; +} + +/** + * Performs equality by iterating through keys on an object and returning false + * when any key has values which are not strictly equal between the arguments. + * Returns true when the values of all keys are strictly equal. + */ +export default function shallowEqual(objA, objB) { + if (is(objA, objB)) { + return true; + } + + if ( + typeof objA !== 'object' || + objA === null || + typeof objB !== 'object' || + objB === null + ) { + return false; + } + + const keysA = Object.keys(objA); + const keysB = Object.keys(objB); + + if (keysA.length !== keysB.length) { + return false; + } + + // Test for A's keys different from B. + for (let i = 0; i < keysA.length; i++) { + if ( + !hasOwnProperty.call(objB, keysA[i]) || + !is(objA[keysA[i]], objB[keysA[i]]) + ) { + return false; + } + } + + return true; +} diff --git a/src/translate.js b/src/translate.js index 7aae234e5..22903c217 100644 --- a/src/translate.js +++ b/src/translate.js @@ -1,6 +1,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import hoistStatics from 'hoist-non-react-statics'; +import shallowEqual from './shallowEqual'; import { getDefaults, setDefaults, getI18n, setI18n } from './context'; import I18n from './I18n'; @@ -26,6 +27,14 @@ export default function translate(namespaces, options = {}) { this.getWrappedInstance = this.getWrappedInstance.bind(this); } + shouldComponentUpdate(nextProps) { + if (!this.options.usePureComponent) { + return true; + } + + return !shallowEqual(this.props, nextProps); + } + getWrappedInstance() { if (!this.options.withRef) { // eslint-disable-next-line no-console