Skip to content

Commit

Permalink
Refactor findHostInstance
Browse files Browse the repository at this point in the history
The reconciler shouldn't expose the Fiber data structure. We should pass
the component instance to the reconciler, since the reconciler is the
thing that is supposed to be instancemap aware.
  • Loading branch information
sebmarkbage committed Apr 8, 2018
1 parent 5f7aa2c commit f97ce2e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 49 deletions.
15 changes: 1 addition & 14 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,20 +1147,7 @@ const ReactDOM: Object = {
return (componentOrElement: any);
}

const inst = ReactInstanceMap.get(componentOrElement);
if (inst) {
return DOMRenderer.findHostInstance(inst);
}

if (typeof componentOrElement.render === 'function') {
invariant(false, 'Unable to find node on an unmounted component.');
} else {
invariant(
false,
'Element appears to be neither ReactComponent nor DOMNode. Keys: %s',
Object.keys(componentOrElement),
);
}
return DOMRenderer.findHostInstance(componentOrElement);
},

hydrate(element: React$Node, container: DOMContainer, callback: ?Function) {
Expand Down
8 changes: 1 addition & 7 deletions packages/react-native-renderer/src/NativeMethodsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
throwOnStylesProp,
warnForStyleProps,
} from './NativeMethodsMixinUtils';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';

export default function(
findNodeHandle: any => ?number,
Expand Down Expand Up @@ -129,17 +128,12 @@ export default function(
// We want the instance/wrapper (not the native tag).
let maybeInstance;

const fiber = ReactInstanceMap.get(this);
if (!fiber) {
return;
}

// Fiber errors if findNodeHandle is called for an umounted component.
// Tests using ReactTestRenderer will trigger this case indirectly.
// Mimicking stack behavior, we should silently ignore this case.
// TODO Fix ReactTestRenderer so we can remove this try/catch.
try {
maybeInstance = findHostInstance(fiber);
maybeInstance = findHostInstance(this);
} catch (error) {}

// If there is no host component beneath this we should fail silently.
Expand Down
9 changes: 1 addition & 8 deletions packages/react-native-renderer/src/ReactFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import {getInspectorDataForViewTag} from './ReactNativeFiberInspector';
import createReactNativeComponentClass from './createReactNativeComponentClass';

import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
import type {Fiber} from 'react-reconciler/src/ReactFiber';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';
import getComponentName from 'shared/getComponentName';
import invariant from 'fbjs/lib/invariant';
import warning from 'fbjs/lib/warning';

const findHostInstance = ReactFabricRenderer.findHostInstance;
Expand Down Expand Up @@ -64,11 +61,7 @@ function findNodeHandle(componentOrHandle: any): ?number {
if (componentOrHandle.canonical && componentOrHandle.canonical._nativeTag) {
return componentOrHandle.canonical._nativeTag;
}
const internalInstance: Fiber = ReactInstanceMap.get(componentOrHandle);
if (!internalInstance) {
return null;
}
const hostInstance = findHostInstance(internalInstance);
const hostInstance = findHostInstance(componentOrHandle);
if (hostInstance == null) {
return hostInstance;
}
Expand Down
8 changes: 1 addition & 7 deletions packages/react-native-renderer/src/ReactNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import UIManager from 'UIManager';

import * as ReactNativeAttributePayload from './ReactNativeAttributePayload';
import {mountSafeCallback} from './NativeMethodsMixinUtils';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';

export default function(
findNodeHandle: any => ?number,
Expand Down Expand Up @@ -140,17 +139,12 @@ export default function(
// We want the instance/wrapper (not the native tag).
let maybeInstance;

const fiber = ReactInstanceMap.get(this);
if (!fiber) {
return;
}

// Fiber errors if findNodeHandle is called for an umounted component.
// Tests using ReactTestRenderer will trigger this case indirectly.
// Mimicking stack behavior, we should silently ignore this case.
// TODO Fix ReactTestRenderer so we can remove this try/catch.
try {
maybeInstance = findHostInstance(fiber);
maybeInstance = findHostInstance(this);
} catch (error) {}

// If there is no host component beneath this we should fail silently.
Expand Down
9 changes: 1 addition & 8 deletions packages/react-native-renderer/src/ReactNativeRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ import {getInspectorDataForViewTag} from './ReactNativeFiberInspector';
import createReactNativeComponentClass from './createReactNativeComponentClass';

import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
import type {Fiber} from 'react-reconciler/src/ReactFiber';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';
import getComponentName from 'shared/getComponentName';
import invariant from 'fbjs/lib/invariant';
import warning from 'fbjs/lib/warning';

const findHostInstance = ReactNativeFiberRenderer.findHostInstance;
Expand Down Expand Up @@ -68,11 +65,7 @@ function findNodeHandle(componentOrHandle: any): ?number {
if (componentOrHandle.canonical && componentOrHandle.canonical._nativeTag) {
return componentOrHandle.canonical._nativeTag;
}
const internalInstance: Fiber = ReactInstanceMap.get(componentOrHandle);
if (!internalInstance) {
return null;
}
const hostInstance = findHostInstance(internalInstance);
const hostInstance = findHostInstance(componentOrHandle);
if (hostInstance == null) {
return hostInstance;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/react-noop-renderer/src/ReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {UpdateQueue} from 'react-reconciler/src/ReactFiberUpdateQueue';
import type {ReactNodeList} from 'shared/ReactTypes';
import ReactFiberReconciler from 'react-reconciler';
import {enablePersistentReconciler} from 'shared/ReactFeatureFlags';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';
import * as ReactPortal from 'shared/ReactPortal';
import emptyObject from 'fbjs/lib/emptyObject';
import expect from 'expect';
Expand Down Expand Up @@ -401,8 +400,7 @@ const ReactNoop = {
if (typeof component.id === 'number') {
return component;
}
const inst = ReactInstanceMap.get(component);
return inst ? NoopRenderer.findHostInstance(inst) : null;
return NoopRenderer.findHostInstance(component);
},

flushDeferredPri(timeout: number = Infinity): Array<mixed> {
Expand Down
17 changes: 15 additions & 2 deletions packages/react-reconciler/src/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as ReactInstanceMap from 'shared/ReactInstanceMap';
import {HostComponent} from 'shared/ReactTypeOfWork';
import emptyObject from 'fbjs/lib/emptyObject';
import getComponentName from 'shared/getComponentName';
import invariant from 'fbjs/lib/invariant';
import warning from 'fbjs/lib/warning';

import {createFiberRoot} from './ReactFiberRoot';
Expand Down Expand Up @@ -264,7 +265,7 @@ export type Reconciler<C, I, TI> = {
): React$Component<any, any> | TI | I | null,

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

// Used internally for filtering out portals. Legacy API.
findHostInstanceWithNoPortals(component: Fiber): I | TI | null,
Expand Down Expand Up @@ -402,7 +403,19 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
);
}

function findHostInstance(fiber: Fiber): PI | null {
function findHostInstance(component: Object): PI | null {
const fiber = ReactInstanceMap.get(component);
if (fiber === undefined) {
if (typeof component.render === 'function') {
invariant(false, 'Unable to find node on an unmounted component.');
} else {
invariant(
false,
'Argument appears to not be a ReactComponent. Keys: %s',
Object.keys(component),
);
}
}
const hostFiber = findCurrentHostFiber(fiber);
if (hostFiber === null) {
return null;
Expand Down

0 comments on commit f97ce2e

Please sign in to comment.