UNPKG

1.28 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11/**
12 * Given a `prevElement` and `nextElement`, determines if the existing
13 * instance should be updated as opposed to being destroyed or replaced by a new
14 * instance. Both arguments are elements. This ensures that this logic can
15 * operate on stateless trees without any backing instance.
16 *
17 * @param {?object} prevElement
18 * @param {?object} nextElement
19 * @return {boolean} True if the existing instance should be updated.
20 * @protected
21 */
22
23function shouldUpdateReactComponent(prevElement, nextElement) {
24 var prevEmpty = prevElement === null || prevElement === false;
25 var nextEmpty = nextElement === null || nextElement === false;
26 if (prevEmpty || nextEmpty) {
27 return prevEmpty === nextEmpty;
28 }
29
30 var prevType = typeof prevElement;
31 var nextType = typeof nextElement;
32 if (prevType === 'string' || prevType === 'number') {
33 return nextType === 'string' || nextType === 'number';
34 } else {
35 return nextType === 'object' && prevElement.type === nextElement.type && prevElement.key === nextElement.key;
36 }
37}
38
39module.exports = shouldUpdateReactComponent;
\No newline at end of file