Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -3045,19 +3045,19 @@ export function updateFragmentInstanceFiber(
}

export function commitNewChildToFragmentInstance(
childElement: Instance,
childInstance: Instance,
fragmentInstance: FragmentInstanceType,
): void {
const eventListeners = fragmentInstance._eventListeners;
if (eventListeners !== null) {
for (let i = 0; i < eventListeners.length; i++) {
const {type, listener, optionsOrUseCapture} = eventListeners[i];
childElement.addEventListener(type, listener, optionsOrUseCapture);
childInstance.addEventListener(type, listener, optionsOrUseCapture);
}
}
if (fragmentInstance._observers !== null) {
fragmentInstance._observers.forEach(observer => {
observer.observe(childElement);
observer.observe(childInstance);
});
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/react-native-renderer/src/ReactFiberConfigFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,17 @@ export function updateFragmentInstanceFiber(
}

export function commitNewChildToFragmentInstance(
child: Fiber,
childInstance: Instance,
fragmentInstance: FragmentInstanceType,
): void {
const publicInstance = getPublicInstance(childInstance);
if (fragmentInstance._observers !== null) {
if (publicInstance == null) {
throw new Error('Expected to find a host node. This is a bug in React.');
}
fragmentInstance._observers.forEach(observer => {
observeChild(child, observer);
// $FlowFixMe[incompatible-call] Element types are behind a flag in RN
observer.observe(publicInstance);
});
}
}
Expand Down
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']);
});
});
});
72 changes: 51 additions & 21 deletions packages/react-reconciler/src/ReactFiberCommitHostEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,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 @@ -384,14 +392,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 @@ -449,14 +450,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 @@ -494,10 +488,6 @@ function insertOrAppendPlacementNode(
}

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

// Recursively insert all host nodes into the parent.
let hostParentFiber;
let parentFragmentInstances = null;
Expand All @@ -517,6 +507,17 @@ function commitPlacement(finishedWork: Fiber): void {
}
parentFiber = parentFiber.return;
}

if (!supportsMutation) {
if (enableFragmentRefs) {
appendImmutableNodeToFragmentInstances(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what supportsMutation means, but wanted to point out that we are appending something in the fragment refs path which might be considered a mutation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

append isn't great naming here. I'll update.

supportsMutation refers to mutation mode https://github.com/facebook/react/blob/main/packages/react-reconciler/README.md#modes

In this case, we're targeting Fabric behavior which does not support mutation.

The append isn't an append to a mutable tree. Its more like letting the parent fragment know it has a new child, independent of the actual insert of that node.

finishedWork,
parentFragmentInstances,
);
}
return;
}

if (hostParentFiber == null) {
throw new Error(
'Expected to find a host parent. This error is likely caused by a bug ' +
Expand Down Expand Up @@ -581,6 +582,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