Skip to content

New children notify fragment instances in Fabric #33093

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,46 @@ describe('Fabric FragmentRefs', () => {

expect(fragmentRef && fragmentRef._fragmentFiber).toBeTruthy();
});

describe('observers', () => {
// @gate enableFragmentRefs
it('observes children, newly added children', async () => {
let logs = [];
const observer = {
observe: entry => {
// Here we reference internals because we don't need to mock the native observer
// We only need to test that each child node is observed on insertion
logs.push(entry.__internalInstanceHandle.pendingProps.nativeID);
},
};
function Test({showB}) {
const fragmentRef = React.useRef(null);
React.useEffect(() => {
fragmentRef.current.observeUsing(observer);
const lastRefValue = fragmentRef.current;
return () => {
lastRefValue.unobserveUsing(observer);
};
}, []);
return (
<View nativeID="parent">
<React.Fragment ref={fragmentRef}>
<View nativeID="A" />
{showB && <View nativeID="B" />}
</React.Fragment>
</View>
);
}

await act(() => {
ReactFabric.render(<Test showB={false} />, 11, null, true);
});
expect(logs).toEqual(['A']);
logs = [];
await act(() => {
ReactFabric.render(<Test showB={true} />, 11, null, true);
});
expect(logs).toEqual(['B']);
});
});
});
66 changes: 48 additions & 18 deletions packages/react-reconciler/src/ReactFiberCommitHostEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,16 @@ export function commitShowHideHostTextInstance(node: Fiber, isHidden: boolean) {

export function commitNewChildToFragmentInstances(
fiber: Fiber,
parentFragmentInstances: Array<FragmentInstanceType>,
parentFragmentInstances: null | Array<FragmentInstanceType>,
): void {
if (
fiber.tag !== HostComponent ||
// Only run fragment insertion effects for initial insertions
fiber.alternate !== null ||
parentFragmentInstances === null
) {
return;
}
for (let i = 0; i < parentFragmentInstances.length; i++) {
const fragmentInstance = parentFragmentInstances[i];
commitNewChildToFragmentInstance(fiber.stateNode, fragmentInstance);
Expand Down Expand Up @@ -361,14 +369,7 @@ function insertOrAppendPlacementNodeIntoContainer(
} else {
appendChildToContainer(parent, stateNode);
}
// TODO: Enable HostText for RN
if (
enableFragmentRefs &&
tag === HostComponent &&
// Only run fragment insertion effects for initial insertions
node.alternate === null &&
parentFragmentInstances !== null
) {
if (enableFragmentRefs) {
commitNewChildToFragmentInstances(node, parentFragmentInstances);
}
trackHostMutation();
Expand Down Expand Up @@ -426,14 +427,7 @@ function insertOrAppendPlacementNode(
} else {
appendChild(parent, stateNode);
}
// TODO: Enable HostText for RN
if (
enableFragmentRefs &&
tag === HostComponent &&
// Only run fragment insertion effects for initial insertions
node.alternate === null &&
parentFragmentInstances !== null
) {
if (enableFragmentRefs) {
commitNewChildToFragmentInstances(node, parentFragmentInstances);
}
trackHostMutation();
Expand Down Expand Up @@ -471,7 +465,7 @@ function insertOrAppendPlacementNode(
}

function commitPlacement(finishedWork: Fiber): void {
if (!supportsMutation) {
if (!supportsMutation && !enableFragmentRefs) {
return;
}

Expand Down Expand Up @@ -500,6 +494,13 @@ function commitPlacement(finishedWork: Fiber): void {
'in React. Please file an issue.',
);
}
if (!supportsMutation && enableFragmentRefs) {
appendImmutableNodeToFragmentInstances(
finishedWork,
parentFragmentInstances,
);
return;
}

switch (hostParentFiber.tag) {
case HostSingleton: {
Expand Down Expand Up @@ -558,6 +559,35 @@ function commitPlacement(finishedWork: Fiber): void {
}
}

function appendImmutableNodeToFragmentInstances(
finishedWork: Fiber,
parentFragmentInstances: null | Array<FragmentInstanceType>,
): void {
if (!enableFragmentRefs) {
return;
}
const isHost = finishedWork.tag === HostComponent;
if (isHost) {
commitNewChildToFragmentInstances(finishedWork, parentFragmentInstances);
return;
} else if (finishedWork.tag === HostPortal) {
// If the insertion itself is a portal, then we don't want to traverse
// down its children. Instead, we'll get insertions from each child in
// the portal directly.
return;
}

const child = finishedWork.child;
if (child !== null) {
appendImmutableNodeToFragmentInstances(child, parentFragmentInstances);
let sibling = child.sibling;
while (sibling !== null) {
appendImmutableNodeToFragmentInstances(sibling, parentFragmentInstances);
sibling = sibling.sibling;
}
}
}

export function commitHostPlacement(finishedWork: Fiber) {
try {
if (__DEV__) {
Expand Down
Loading