Skip to content

Commit f9edf89

Browse files
committed
Fork findHostInstance to make findHostInstanceWithNoPortals
**what is the change?:** We need to get host instances, but filter out portals. There is not currently a method for that. **why make this change?:** Rather than change the existing `findHostInstance` method, which would affect the behavior of the public `findDOMNode` method, we are forking. **test plan:** `yarn test` **issue:** facebook#8854
1 parent 178e1a4 commit f9edf89

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/renderers/dom/fiber/ReactDOMFiberEntry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ function renderSubtreeIntoContainer(
536536

537537
if (__DEV__) {
538538
if (container._reactRootContainer && container.nodeType !== COMMENT_NODE) {
539-
const hostInstance = DOMRenderer.findHostInstance(
539+
const hostInstance = DOMRenderer.findHostInstanceWithNoPortals(
540540
container._reactRootContainer.current,
541541
);
542542
if (hostInstance) {

src/renderers/shared/fiber/ReactFiberReconciler.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ if (__DEV__) {
3737
var getComponentName = require('getComponentName');
3838
}
3939

40-
var {findCurrentHostFiber} = require('ReactFiberTreeReflection');
40+
var {
41+
findCurrentHostFiber,
42+
findCurrentHostFiberWithNoPortals,
43+
} = require('ReactFiberTreeReflection');
4144

4245
var getContextForSubtree = require('getContextForSubtree');
4346

@@ -173,6 +176,9 @@ export type Reconciler<C, I, TI> = {
173176

174177
// Use for findDOMNode/findHostNode. Legacy API.
175178
findHostInstance(component: Fiber): I | TI | null,
179+
180+
// Used internally for filtering out portals. Legacy API.
181+
findHostInstanceWithNoPortals(component: Fiber): I | TI | null,
176182
};
177183

178184
getContextForSubtree._injectFiber(function(fiber: Fiber) {
@@ -309,5 +315,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
309315
}
310316
return hostFiber.stateNode;
311317
},
318+
319+
findHostInstanceWithNoPortals(fiber: Fiber): PI | null {
320+
const hostFiber = findCurrentHostFiberWithNoPortals(fiber);
321+
if (hostFiber === null) {
322+
return null;
323+
}
324+
return hostFiber.stateNode;
325+
},
312326
};
313327
};

src/renderers/shared/fiber/ReactFiberTreeReflection.js

+35
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,41 @@ exports.findCurrentHostFiber = function(parent: Fiber): Fiber | null {
237237
return null;
238238
}
239239

240+
// Next we'll drill down this component to find the first HostComponent/Text.
241+
let node: Fiber = currentParent;
242+
while (true) {
243+
if (node.tag === HostComponent || node.tag === HostText) {
244+
return node;
245+
} else if (node.child) {
246+
node.child.return = node;
247+
node = node.child;
248+
continue;
249+
}
250+
if (node === currentParent) {
251+
return null;
252+
}
253+
while (!node.sibling) {
254+
if (!node.return || node.return === currentParent) {
255+
return null;
256+
}
257+
node = node.return;
258+
}
259+
node.sibling.return = node.return;
260+
node = node.sibling;
261+
}
262+
// Flow needs the return null here, but ESLint complains about it.
263+
// eslint-disable-next-line no-unreachable
264+
return null;
265+
};
266+
267+
exports.findCurrentHostFiberWithNoPortals = function(
268+
parent: Fiber,
269+
): Fiber | null {
270+
const currentParent = findCurrentFiberUsingSlowPath(parent);
271+
if (!currentParent) {
272+
return null;
273+
}
274+
240275
// Next we'll drill down this component to find the first HostComponent/Text.
241276
let node: Fiber = currentParent;
242277
while (true) {

0 commit comments

Comments
 (0)