Skip to content

Commit bc753a7

Browse files
authored
Support findNodeHandle in Fabric (#12573)
This doesn't actually need to share any state because it goes through the instance to the fiber structure. Since Fabric is on the same version as RN, calling it on either renderer works.
1 parent 6bf2797 commit bc753a7

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

packages/react-native-renderer/src/ReactFabric.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import ReactFabricRenderer from './ReactFabricRenderer';
2424
import ReactNativePropRegistry from './ReactNativePropRegistry';
2525
import {getInspectorDataForViewTag} from './ReactNativeFiberInspector';
2626
import createReactNativeComponentClass from './createReactNativeComponentClass';
27-
import {injectFindHostInstanceFabric} from './findNodeHandle';
27+
import {injectFindHostInstance} from './findNodeHandle';
2828
import findNumericNodeHandle from './findNumericNodeHandle';
2929
import takeSnapshot from './takeSnapshot';
3030

31-
injectFindHostInstanceFabric(ReactFabricRenderer.findHostInstance);
31+
injectFindHostInstance(ReactFabricRenderer.findHostInstance);
3232

3333
ReactGenericBatching.injection.injectRenderer(ReactFabricRenderer);
3434

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
* @emails react-core
8+
* @jest-environment node
9+
*/
10+
11+
'use strict';
12+
13+
let React;
14+
let ReactFabric;
15+
let ReactNative;
16+
let createReactNativeComponentClass;
17+
18+
describe('ReactFabric', () => {
19+
beforeEach(() => {
20+
jest.resetModules();
21+
ReactNative = require('react-native-renderer');
22+
jest.resetModules();
23+
jest.mock('shared/ReactFeatureFlags', () =>
24+
require('shared/forks/ReactFeatureFlags.native-fabric'),
25+
);
26+
27+
React = require('react');
28+
ReactFabric = require('react-native-renderer/fabric');
29+
createReactNativeComponentClass = require('../createReactNativeComponentClass')
30+
.default;
31+
});
32+
33+
it('find Fabric nodes with the RN renderer', () => {
34+
const View = createReactNativeComponentClass('View', () => ({
35+
validAttributes: {title: true},
36+
uiViewClassName: 'View',
37+
}));
38+
39+
let ref = React.createRef();
40+
41+
class Component extends React.Component {
42+
render() {
43+
return <View title="foo" />;
44+
}
45+
}
46+
47+
ReactFabric.render(<Component ref={ref} />, 11);
48+
49+
let handle = ReactNative.findNodeHandle(ref.current);
50+
expect(handle).toBe(2);
51+
});
52+
});

packages/react-native-renderer/src/findNodeHandle.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,14 @@ import getComponentName from 'shared/getComponentName';
1515
import invariant from 'fbjs/lib/invariant';
1616
import warning from 'fbjs/lib/warning';
1717

18-
// TODO: Share this module between Fabric and React Native renderers
19-
// so that both can be used in the same tree.
20-
2118
let findHostInstance = function(fiber: Fiber): any {
2219
return null;
2320
};
2421

25-
let findHostInstanceFabric = function(fiber: Fiber): any {
26-
return null;
27-
};
28-
2922
export function injectFindHostInstance(impl: (fiber: Fiber) => any) {
3023
findHostInstance = impl;
3124
}
3225

33-
export function injectFindHostInstanceFabric(impl: (fiber: Fiber) => any) {
34-
findHostInstanceFabric = impl;
35-
}
36-
3726
/**
3827
* ReactNative vs ReactWeb
3928
* -----------------------
@@ -98,24 +87,11 @@ function findNodeHandle(componentOrHandle: any): any {
9887
// ReactInstanceMap.get here will always succeed for mounted components
9988
const internalInstance: Fiber = ReactInstanceMap.get(component);
10089
if (internalInstance) {
101-
return (
102-
findHostInstance(internalInstance) ||
103-
findHostInstanceFabric(internalInstance)
104-
);
90+
return findHostInstance(internalInstance);
10591
} else {
10692
if (component) {
10793
return component;
10894
} else {
109-
invariant(
110-
// Native
111-
(typeof component === 'object' && '_nativeTag' in component) ||
112-
// Composite
113-
(component.render != null && typeof component.render === 'function'),
114-
'findNodeHandle(...): Argument is not a component ' +
115-
'(type: %s, keys: %s)',
116-
typeof component,
117-
Object.keys(component),
118-
);
11995
invariant(
12096
false,
12197
'findNodeHandle(...): Unable to find node handle for unmounted ' +

packages/react-native-renderer/src/findNumericNodeHandle.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ export default function findNumericNodeHandleFiber(
2121
if (instance == null || typeof instance === 'number') {
2222
return instance;
2323
}
24+
if (instance.canonical) {
25+
return instance.canonical._nativeTag;
26+
}
2427
return instance._nativeTag;
2528
}

0 commit comments

Comments
 (0)