main
js 55 lines 1.54 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 strict-local
8 */
9
10 import type {
11 PublicInstance,
12 Instance,
13 Props,
14 TextInstance,
15 } from './ReactFiberConfigFabric';
16 import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
17 import {getPublicInstance} from './ReactFiberConfigFabric';
18
19 // `node` is typed incorrectly here. The proper type should be `PublicInstance`.
20 // This is ok in DOM because they types are interchangeable, but in React Native
21 // they aren't.
22 function getInstanceFromNode(node: Instance | TextInstance): Fiber | null {
23 const instance: Instance = node as $FlowFixMe; // In React Native, node is never a text instance
24
25 if (
26 instance.canonical != null &&
27 instance.canonical.internalInstanceHandle != null
28 ) {
29 return instance.canonical.internalInstanceHandle;
30 }
31
32 // $FlowFixMe[incompatible-type] DevTools incorrectly passes a fiber in React Native.
33 return node;
34 }
35
36 function getNodeFromInstance(fiber: Fiber): PublicInstance {
37 const publicInstance = getPublicInstance(fiber.stateNode);
38
39 if (publicInstance == null) {
40 throw new Error('Could not find host instance from fiber');
41 }
42
43 return publicInstance;
44 }
45
46 function getFiberCurrentPropsFromNode(instance: Instance): Props {
47 return instance.canonical.currentProps;
48 }
49
50 export {
51 getInstanceFromNode,
52 getInstanceFromNode as getClosestInstanceFromNode,
53 getNodeFromInstance,
54 getFiberCurrentPropsFromNode,
55 };