Skip to content

Commit

Permalink
Fork findHostInstance to make findHostInstanceWithNoPortals
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
flarnie committed Jul 20, 2017
1 parent 178e1a4 commit f9edf89
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/renderers/dom/fiber/ReactDOMFiberEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ function renderSubtreeIntoContainer(

if (__DEV__) {
if (container._reactRootContainer && container.nodeType !== COMMENT_NODE) {
const hostInstance = DOMRenderer.findHostInstance(
const hostInstance = DOMRenderer.findHostInstanceWithNoPortals(
container._reactRootContainer.current,
);
if (hostInstance) {
Expand Down
16 changes: 15 additions & 1 deletion src/renderers/shared/fiber/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ if (__DEV__) {
var getComponentName = require('getComponentName');
}

var {findCurrentHostFiber} = require('ReactFiberTreeReflection');
var {
findCurrentHostFiber,
findCurrentHostFiberWithNoPortals,
} = require('ReactFiberTreeReflection');

var getContextForSubtree = require('getContextForSubtree');

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

// Use for findDOMNode/findHostNode. Legacy API.
findHostInstance(component: Fiber): I | TI | null,

// Used internally for filtering out portals. Legacy API.
findHostInstanceWithNoPortals(component: Fiber): I | TI | null,
};

getContextForSubtree._injectFiber(function(fiber: Fiber) {
Expand Down Expand Up @@ -309,5 +315,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
}
return hostFiber.stateNode;
},

findHostInstanceWithNoPortals(fiber: Fiber): PI | null {
const hostFiber = findCurrentHostFiberWithNoPortals(fiber);
if (hostFiber === null) {
return null;
}
return hostFiber.stateNode;
},
};
};
35 changes: 35 additions & 0 deletions src/renderers/shared/fiber/ReactFiberTreeReflection.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,41 @@ exports.findCurrentHostFiber = function(parent: Fiber): Fiber | null {
return null;
}

// Next we'll drill down this component to find the first HostComponent/Text.
let node: Fiber = currentParent;
while (true) {
if (node.tag === HostComponent || node.tag === HostText) {
return node;
} else if (node.child) {
node.child.return = node;
node = node.child;
continue;
}
if (node === currentParent) {
return null;
}
while (!node.sibling) {
if (!node.return || node.return === currentParent) {
return null;
}
node = node.return;
}
node.sibling.return = node.return;
node = node.sibling;
}
// Flow needs the return null here, but ESLint complains about it.
// eslint-disable-next-line no-unreachable
return null;
};

exports.findCurrentHostFiberWithNoPortals = function(
parent: Fiber,
): Fiber | null {
const currentParent = findCurrentFiberUsingSlowPath(parent);
if (!currentParent) {
return null;
}

// Next we'll drill down this component to find the first HostComponent/Text.
let node: Fiber = currentParent;
while (true) {
Expand Down

0 comments on commit f9edf89

Please sign in to comment.