Type ReactRef (#7600)
Nothing out of the ordinary on this one.
Christopher Chedeau committed
Sep 2, 2016 at 14:16 UTC
31dd6944d34f404ec1caabc54c53f7f79d616214
1 file changed
+33
-12
src/renderers/shared/stack/reconciler/ReactRef.js
+33
-12
@@ -7,12 +7,16 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule ReactRef
10
+ * @flow
11
*/
12
13
'use strict';
14
15
var ReactOwner = require('ReactOwner');
16
17
+import type { ReactInstance } from 'ReactInstanceType';
18
+import type { ReactElement } from 'ReactElementType';
19
+
20
var ReactRef = {};
21
22
function attachRef(ref, component, owner) {
@@ -33,8 +37,11 @@ function detachRef(ref, component, owner) {
37
}
38
}
39
36
-ReactRef.attachRefs = function(instance, element) {
37
- if (element === null || element === false) {
40
+ReactRef.attachRefs = function(
41
+ instance: ReactInstance,
42
+ element: ReactElement | string | number | null | false,
43
+): void {
44
+ if (element === null || typeof element !== 'object') {
45
return;
46
}
47
var ref = element.ref;
@@ -43,7 +50,10 @@ ReactRef.attachRefs = function(instance, element) {
50
}
51
};
52
46
-ReactRef.shouldUpdateRefs = function(prevElement, nextElement) {
53
+ReactRef.shouldUpdateRefs = function(
54
+ prevElement: ReactElement | string | number | null | false,
55
+ nextElement: ReactElement | string | number | null | false,
56
+): bool {
57
// If either the owner or a `ref` has changed, make sure the newest owner
58
// has stored a reference to `this`, and the previous owner (if different)
59
// has forgotten the reference to `this`. We use the element instead
@@ -56,21 +66,32 @@ ReactRef.shouldUpdateRefs = function(prevElement, nextElement) {
66
// is made. It probably belongs where the key checking and
67
// instantiateReactComponent is done.
68
59
- var prevEmpty = prevElement === null || prevElement === false;
60
- var nextEmpty = nextElement === null || nextElement === false;
69
+ var prevRef = null;
70
+ var prevOwner = null;
71
+ if (prevElement !== null && typeof prevElement === 'object') {
72
+ prevRef = prevElement.ref;
73
+ prevOwner = prevElement._owner;
74
+ }
75
+
76
+ var nextRef = null;
77
+ var nextOwner = null;
78
+ if (nextElement !== null && typeof nextElement === 'object') {
79
+ nextRef = nextElement.ref;
80
+ nextOwner = nextElement._owner;
81
+ }
82
83
return (
63
- // This has a few false positives w/r/t empty components.
64
- prevEmpty || nextEmpty ||
65
- nextElement.ref !== prevElement.ref ||
84
+ prevRef !== nextRef ||
85
// If owner changes but we have an unchanged function ref, don't update refs
67
- (typeof nextElement.ref === 'string' &&
68
- nextElement._owner !== prevElement._owner)
86
+ (typeof nextRef === 'string' && nextOwner !== prevOwner)
87
);
88
};
89
72
-ReactRef.detachRefs = function(instance, element) {
73
- if (element === null || element === false) {
90
+ReactRef.detachRefs = function(
91
+ instance: ReactInstance,
92
+ element: ReactElement | string | number | null | false,
93
+): void {
94
+ if (element === null || typeof element !== 'object') {
95
return;
96
}
97
var ref = element.ref;