Flow type ReactDOMComponentTree (#18280)
Dominic Gannaway committed
Mar 11, 2020 at 19:38 UTC
8b155d261368afb1fe93219278b581a1032ea1d7
4 files changed
+40
-19
packages/react-dom/src/client/ReactDOMComponentTree.js
+37
-16
@@ -3,8 +3,19 @@
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
+ * @flow
8
*/
9
10
+import type {Fiber} from 'react-reconciler/src/ReactFiber';
11
+import type {
12
+ Container,
13
+ TextInstance,
14
+ Instance,
15
+ SuspenseInstance,
16
+ Props,
17
+} from './ReactDOMHostConfig';
18
+
19
import {
20
HostComponent,
21
HostText,
@@ -22,19 +33,22 @@ const internalInstanceKey = '__reactInternalInstance$' + randomKey;
33
const internalEventHandlersKey = '__reactEventHandlers$' + randomKey;
34
const internalContainerInstanceKey = '__reactContainere$' + randomKey;
35
25
-export function precacheFiberNode(hostInst, node) {
26
- node[internalInstanceKey] = hostInst;
36
+export function precacheFiberNode(
37
+ hostInst: Fiber,
38
+ node: Instance | TextInstance | SuspenseInstance,
39
+): void {
40
+ (node: any)[internalInstanceKey] = hostInst;
41
}
42
29
-export function markContainerAsRoot(hostRoot, node) {
43
+export function markContainerAsRoot(hostRoot: Fiber, node: Container): void {
44
node[internalContainerInstanceKey] = hostRoot;
45
}
46
33
-export function unmarkContainerAsRoot(node) {
47
+export function unmarkContainerAsRoot(node: Container): void {
48
node[internalContainerInstanceKey] = null;
49
}
50
37
-export function isContainerMarkedAsRoot(node) {
51
+export function isContainerMarkedAsRoot(node: Container): boolean {
52
return !!node[internalContainerInstanceKey];
53
}
54
@@ -45,8 +59,8 @@ export function isContainerMarkedAsRoot(node) {
59
// pass the Container node as the targetNode, you will not actually get the
60
// HostRoot back. To get to the HostRoot, you need to pass a child of it.
61
// The same thing applies to Suspense boundaries.
48
-export function getClosestInstanceFromNode(targetNode) {
49
- let targetInst = targetNode[internalInstanceKey];
62
+export function getClosestInstanceFromNode(targetNode: Node): null | Fiber {
63
+ let targetInst = (targetNode: any)[internalInstanceKey];
64
if (targetInst) {
65
// Don't return HostRoot or SuspenseComponent here.
66
return targetInst;
@@ -64,8 +78,8 @@ export function getClosestInstanceFromNode(targetNode) {
78
// node and the first child. It isn't surrounding the container node.
79
// If it's not a container, we check if it's an instance.
80
targetInst =
67
- parentNode[internalContainerInstanceKey] ||
68
- parentNode[internalInstanceKey];
81
+ (parentNode: any)[internalContainerInstanceKey] ||
82
+ (parentNode: any)[internalInstanceKey];
83
if (targetInst) {
84
// Since this wasn't the direct target of the event, we might have
85
// stepped past dehydrated DOM nodes to get here. However they could
@@ -124,8 +138,10 @@ export function getClosestInstanceFromNode(targetNode) {
138
* Given a DOM node, return the ReactDOMComponent or ReactDOMTextComponent
139
* instance, or null if the node was not rendered by this React.
140
*/
127
-export function getInstanceFromNode(node) {
128
- const inst = node[internalInstanceKey] || node[internalContainerInstanceKey];
141
+export function getInstanceFromNode(node: Node): Fiber | null {
142
+ const inst =
143
+ (node: any)[internalInstanceKey] ||
144
+ (node: any)[internalContainerInstanceKey];
145
if (inst) {
146
if (
147
inst.tag === HostComponent ||
@@ -145,7 +161,7 @@ export function getInstanceFromNode(node) {
161
* Given a ReactDOMComponent or ReactDOMTextComponent, return the corresponding
162
* DOM node.
163
*/
148
-export function getNodeFromInstance(inst) {
164
+export function getNodeFromInstance(inst: Fiber): Instance | TextInstance {
165
if (inst.tag === HostComponent || inst.tag === HostText) {
166
// In Fiber this, is just the state node right now. We assume it will be
167
// a host component or host text.
@@ -157,10 +173,15 @@ export function getNodeFromInstance(inst) {
173
invariant(false, 'getNodeFromInstance: Invalid argument.');
174
}
175
160
-export function getFiberCurrentPropsFromNode(node) {
161
- return node[internalEventHandlersKey] || null;
176
+export function getFiberCurrentPropsFromNode(
177
+ node: Instance | TextInstance | SuspenseInstance,
178
+): Props {
179
+ return (node: any)[internalEventHandlersKey] || null;
180
}
181
164
-export function updateFiberProps(node, props) {
165
- node[internalEventHandlersKey] = props;
182
+export function updateFiberProps(
183
+ node: Instance | TextInstance | SuspenseInstance,
184
+ props: Props,
185
+): void {
186
+ (node: any)[internalEventHandlersKey] = props;
187
}
packages/react-dom/src/client/ReactDOMHostConfig.js
+1
-1
@@ -824,7 +824,7 @@ export function getNextHydratableInstanceAfterSuspenseInstance(
824
// SuspenseInstance. I.e. if its previous sibling is a Comment with
825
// SUSPENSE_x_START_DATA. Otherwise, null.
826
export function getParentSuspenseInstance(
827
- targetInstance: Instance,
827
+ targetInstance: Node,
828
): null | SuspenseInstance {
829
let node = targetInstance.previousSibling;
830
// Skip past all nodes within this suspense boundary.
packages/react-reconciler/src/ReactFiberReconciler.js
+1
-1
@@ -104,7 +104,7 @@ type DevToolsConfig = {|
104
rendererPackageName: string,
105
// Note: this actually *does* depend on Fiber internal fields.
106
// Used by "inspect clicked DOM element" in React DevTools.
107
- findFiberByHostInstance?: (instance: Instance | TextInstance) => Fiber,
107
+ findFiberByHostInstance?: (instance: Instance | TextInstance) => Fiber | null,
108
// Used by RN in-app inspector.
109
// This API is unfortunately RN-specific.
110
// TODO: Change it to accept Fiber instead and type it properly.
packages/shared/ReactTypes.js
+1
-1
@@ -124,7 +124,7 @@ export const UserBlockingEvent: EventPriority = 1;
124
export const ContinuousEvent: EventPriority = 2;
125
126
export type ReactFundamentalComponentInstance<C, H> = {|
127
- currentFiber: mixed,
127
+ currentFiber: Object,
128
instance: mixed,
129
prevProps: null | Object,
130
props: Object,