UNPKG

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