Skip to content

Commit ffe0bd0

Browse files
committed
Notify fragment instance of added/removed text nodes
1 parent 17dc288 commit ffe0bd0

4 files changed

Lines changed: 51 additions & 17 deletions

File tree

packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,40 +3538,48 @@ export function updateFragmentInstanceFiber(
35383538
}
35393539

35403540
export function commitNewChildToFragmentInstance(
3541-
childInstance: InstanceWithFragmentHandles,
3541+
childInstance: InstanceWithFragmentHandles | Text,
35423542
fragmentInstance: FragmentInstanceType,
35433543
): void {
3544+
if (childInstance.nodeType === TEXT_NODE) {
3545+
return;
3546+
}
3547+
const instance: InstanceWithFragmentHandles = (childInstance: any);
35443548
const eventListeners = fragmentInstance._eventListeners;
35453549
if (eventListeners !== null) {
35463550
for (let i = 0; i < eventListeners.length; i++) {
35473551
const {type, listener, optionsOrUseCapture} = eventListeners[i];
3548-
childInstance.addEventListener(type, listener, optionsOrUseCapture);
3552+
instance.addEventListener(type, listener, optionsOrUseCapture);
35493553
}
35503554
}
35513555
if (fragmentInstance._observers !== null) {
35523556
fragmentInstance._observers.forEach(observer => {
3553-
observer.observe(childInstance);
3557+
observer.observe(instance);
35543558
});
35553559
}
35563560
if (enableFragmentRefsInstanceHandles) {
3557-
addFragmentHandleToInstance(childInstance, fragmentInstance);
3561+
addFragmentHandleToInstance(instance, fragmentInstance);
35583562
}
35593563
}
35603564

35613565
export function deleteChildFromFragmentInstance(
3562-
childInstance: InstanceWithFragmentHandles,
3566+
childInstance: InstanceWithFragmentHandles | Text,
35633567
fragmentInstance: FragmentInstanceType,
35643568
): void {
3569+
if (childInstance.nodeType === TEXT_NODE) {
3570+
return;
3571+
}
3572+
const instance: InstanceWithFragmentHandles = (childInstance: any);
35653573
const eventListeners = fragmentInstance._eventListeners;
35663574
if (eventListeners !== null) {
35673575
for (let i = 0; i < eventListeners.length; i++) {
35683576
const {type, listener, optionsOrUseCapture} = eventListeners[i];
3569-
childInstance.removeEventListener(type, listener, optionsOrUseCapture);
3577+
instance.removeEventListener(type, listener, optionsOrUseCapture);
35703578
}
35713579
}
35723580
if (enableFragmentRefsInstanceHandles) {
3573-
if (childInstance.unstable_reactFragments != null) {
3574-
childInstance.unstable_reactFragments.delete(fragmentInstance);
3581+
if (instance.unstable_reactFragments != null) {
3582+
instance.unstable_reactFragments.delete(fragmentInstance);
35753583
}
35763584
}
35773585
}

packages/react-native-renderer/src/ReactFiberConfigFabric.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ import {
4040
type PublicTextInstance,
4141
type PublicRootInstance,
4242
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
43-
import {enableFragmentRefsInstanceHandles} from 'shared/ReactFeatureFlags';
43+
import {
44+
enableFragmentRefsInstanceHandles,
45+
enableFragmentRefsTextNodes,
46+
} from 'shared/ReactFeatureFlags';
4447

4548
const {
4649
createNode,
@@ -847,10 +850,15 @@ export function updateFragmentInstanceFiber(
847850
}
848851

849852
export function commitNewChildToFragmentInstance(
850-
childInstance: Instance,
853+
childInstance: Instance | TextInstance,
851854
fragmentInstance: FragmentInstanceType,
852855
): void {
853-
const publicInstance = getPublicInstance(childInstance);
856+
// Text nodes are not observable
857+
if (enableFragmentRefsTextNodes && childInstance.canonical == null) {
858+
return;
859+
}
860+
const instance: Instance = (childInstance: any);
861+
const publicInstance = getPublicInstance(instance);
854862
if (fragmentInstance._observers !== null) {
855863
if (publicInstance == null) {
856864
throw new Error('Expected to find a host node. This is a bug in React.');
@@ -869,11 +877,16 @@ export function commitNewChildToFragmentInstance(
869877
}
870878

871879
export function deleteChildFromFragmentInstance(
872-
childInstance: Instance,
880+
childInstance: Instance | TextInstance,
873881
fragmentInstance: FragmentInstanceType,
874882
): void {
883+
// Text nodes are not observable
884+
if (enableFragmentRefsTextNodes && childInstance.canonical == null) {
885+
return;
886+
}
887+
const instance: Instance = (childInstance: any);
875888
const publicInstance = ((getPublicInstance(
876-
childInstance,
889+
instance,
877890
): any): PublicInstanceWithFragmentHandles);
878891
if (enableFragmentRefsInstanceHandles) {
879892
if (publicInstance.unstable_reactFragments != null) {

packages/react-reconciler/src/ReactFiberCommitHostEffects.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ import {captureCommitPhaseError} from './ReactFiberWorkLoop';
6464
import {trackHostMutation} from './ReactFiberMutationTracking';
6565

6666
import {runWithFiberInDEV} from './ReactCurrentFiber';
67-
import {enableFragmentRefs} from 'shared/ReactFeatureFlags';
67+
import {
68+
enableFragmentRefs,
69+
enableFragmentRefsTextNodes,
70+
} from 'shared/ReactFeatureFlags';
6871

6972
export function commitHostMount(finishedWork: Fiber) {
7073
const type = finishedWork.type;
@@ -258,7 +261,8 @@ export function commitNewChildToFragmentInstances(
258261
parentFragmentInstances: null | Array<FragmentInstanceType>,
259262
): void {
260263
if (
261-
fiber.tag !== HostComponent ||
264+
(fiber.tag !== HostComponent &&
265+
!(enableFragmentRefsTextNodes && fiber.tag === HostText)) ||
262266
// Only run fragment insertion effects for initial insertions
263267
fiber.alternate !== null ||
264268
parentFragmentInstances === null

packages/react-reconciler/src/ReactFiberCommitWork.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
enableFragmentRefs,
6262
enableEagerAlternateStateNodeCleanup,
6363
enableDefaultTransitionIndicator,
64+
enableFragmentRefsTextNodes,
6465
} from 'shared/ReactFeatureFlags';
6566
import {
6667
FunctionComponent,
@@ -1532,7 +1533,11 @@ function commitDeletionEffectsOnFiber(
15321533
if (!offscreenSubtreeWasHidden) {
15331534
safelyDetachRef(deletedFiber, nearestMountedAncestor);
15341535
}
1535-
if (enableFragmentRefs && deletedFiber.tag === HostComponent) {
1536+
if (
1537+
enableFragmentRefs &&
1538+
(deletedFiber.tag === HostComponent ||
1539+
(enableFragmentRefsTextNodes && deletedFiber.tag === HostText))
1540+
) {
15361541
commitFragmentInstanceDeletionEffects(deletedFiber);
15371542
}
15381543
// Intentional fallthrough to next branch
@@ -3009,7 +3014,11 @@ export function disappearLayoutEffects(finishedWork: Fiber) {
30093014
// TODO (Offscreen) Check: flags & RefStatic
30103015
safelyDetachRef(finishedWork, finishedWork.return);
30113016

3012-
if (enableFragmentRefs && finishedWork.tag === HostComponent) {
3017+
if (
3018+
enableFragmentRefs &&
3019+
(finishedWork.tag === HostComponent ||
3020+
(enableFragmentRefsTextNodes && finishedWork.tag === HostText))
3021+
) {
30133022
commitFragmentInstanceDeletionEffects(finishedWork);
30143023
}
30153024

0 commit comments

Comments
 (0)