Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against unmounted components when accessing public instances on Fabric #27687

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/react-native-renderer/src/ReactFiberConfigFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,21 @@ function getPublicTextInstance(
export function getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle: InternalInstanceHandle,
): null | PublicInstance | PublicTextInstance {
const instance = internalInstanceHandle.stateNode;

// React resets all the fields in the fiber when the component is unmounted
// to prevent memory leaks.
if (instance == null) {
return null;
}

if (internalInstanceHandle.tag === HostText) {
const textInstance: TextInstance = internalInstanceHandle.stateNode;
const textInstance: TextInstance = instance;
return getPublicTextInstance(textInstance, internalInstanceHandle);
}

const instance: Instance = internalInstanceHandle.stateNode;
return getPublicInstance(instance);
const elementInstance: Instance = internalInstanceHandle.stateNode;
return getPublicInstance(elementInstance);
}

export function prepareForCommit(containerInfo: Container): null | Object {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-renderer/src/ReactNativeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export type ReactFabricType = {
): ?Node,
getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle: InternalInstanceHandle,
): PublicInstance | PublicTextInstance,
): PublicInstance | PublicTextInstance | null,
...
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,16 @@ describe('ReactFabric', () => {
internalInstanceHandle,
);
expect(publicInstance).toBe(viewRef);

await act(() => {
ReactFabric.render(null, 1);
});

const publicInstanceAfterUnmount =
ReactFabric.getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle,
);
expect(publicInstanceAfterUnmount).toBe(null);
});

it('getPublicInstanceFromInternalInstanceHandle should provide public instances for HostText', async () => {
Expand Down Expand Up @@ -1153,5 +1163,16 @@ describe('ReactFabric', () => {
ReactNativePrivateInterface.createPublicTextInstance.mock.results[0]
.value;
expect(publicInstance).toBe(expectedPublicInstance);

await act(() => {
ReactFabric.render(null, 1);
});

const publicInstanceAfterUnmount =
ReactFabric.getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle,
);

expect(publicInstanceAfterUnmount).toBe(null);
});
});