Skip to content

Commit 9c0323e

Browse files
authored
Stabilize reactFragments host node handle (facebook#35642)
facebook#34935 Introduced `unstable_reactFragments` handle on DOM nodes to enable caching of Observers. This has been tested in production and is stable so it can be rolled out with the Fragment Refs feature.
1 parent e6f1c33 commit 9c0323e

3 files changed

Lines changed: 21 additions & 29 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export type Instance = Element;
218218
export type TextInstance = Text;
219219

220220
type InstanceWithFragmentHandles = Instance & {
221-
unstable_reactFragments?: Set<FragmentInstanceType>,
221+
reactFragments?: Set<FragmentInstanceType>,
222222
};
223223

224224
declare class ActivityInterface extends Comment {}
@@ -3578,10 +3578,10 @@ function addFragmentHandleToInstance(
35783578
fragmentInstance: FragmentInstanceType,
35793579
): void {
35803580
if (enableFragmentRefsInstanceHandles) {
3581-
if (instance.unstable_reactFragments == null) {
3582-
instance.unstable_reactFragments = new Set();
3581+
if (instance.reactFragments == null) {
3582+
instance.reactFragments = new Set();
35833583
}
3584-
instance.unstable_reactFragments.add(fragmentInstance);
3584+
instance.reactFragments.add(fragmentInstance);
35853585
}
35863586
}
35873587

@@ -3647,8 +3647,8 @@ export function deleteChildFromFragmentInstance(
36473647
}
36483648
}
36493649
if (enableFragmentRefsInstanceHandles) {
3650-
if (instance.unstable_reactFragments != null) {
3651-
instance.unstable_reactFragments.delete(fragmentInstance);
3650+
if (instance.reactFragments != null) {
3651+
instance.reactFragments.delete(fragmentInstance);
36523652
}
36533653
}
36543654
}

packages/react-dom/src/__tests__/ReactDOMFragmentRefs-test.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,18 @@ describe('FragmentRefs', () => {
137137
const childB = document.querySelector('#childB');
138138
const childC = document.querySelector('#childC');
139139

140-
expect(childA.unstable_reactFragments.has(fragmentRef.current)).toBe(true);
141-
expect(childB.unstable_reactFragments.has(fragmentRef.current)).toBe(true);
142-
expect(childC.unstable_reactFragments.has(fragmentRef.current)).toBe(false);
143-
expect(childA.unstable_reactFragments.has(fragmentParentRef.current)).toBe(
144-
true,
145-
);
146-
expect(childB.unstable_reactFragments.has(fragmentParentRef.current)).toBe(
147-
true,
148-
);
149-
expect(childC.unstable_reactFragments.has(fragmentParentRef.current)).toBe(
150-
true,
151-
);
140+
expect(childA.reactFragments.has(fragmentRef.current)).toBe(true);
141+
expect(childB.reactFragments.has(fragmentRef.current)).toBe(true);
142+
expect(childC.reactFragments.has(fragmentRef.current)).toBe(false);
143+
expect(childA.reactFragments.has(fragmentParentRef.current)).toBe(true);
144+
expect(childB.reactFragments.has(fragmentParentRef.current)).toBe(true);
145+
expect(childC.reactFragments.has(fragmentParentRef.current)).toBe(true);
152146

153147
await act(() => root.render(<Test show={true} />));
154148

155149
const childD = document.querySelector('#childD');
156-
expect(childD.unstable_reactFragments.has(fragmentRef.current)).toBe(false);
157-
expect(childD.unstable_reactFragments.has(fragmentParentRef.current)).toBe(
158-
true,
159-
);
150+
expect(childD.reactFragments.has(fragmentRef.current)).toBe(false);
151+
expect(childD.reactFragments.has(fragmentParentRef.current)).toBe(true);
160152
});
161153

162154
describe('focus methods', () => {
@@ -1104,7 +1096,7 @@ describe('FragmentRefs', () => {
11041096
}
11051097
const observer = new IntersectionObserver(entries => {
11061098
entries.forEach(entry => {
1107-
const fragmentInstances = entry.target.unstable_reactFragments;
1099+
const fragmentInstances = entry.target.reactFragments;
11081100
if (fragmentInstances) {
11091101
Array.from(fragmentInstances).forEach(fInstance => {
11101102
const cbs = targetToCallbackMap.get(fInstance) || [];

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export type TextInstance = {
124124
export type HydratableInstance = Instance | TextInstance;
125125
export type PublicInstance = ReactNativePublicInstance;
126126
type PublicInstanceWithFragmentHandles = PublicInstance & {
127-
unstable_reactFragments?: Set<FragmentInstanceType>,
127+
reactFragments?: Set<FragmentInstanceType>,
128128
};
129129
export type Container = {
130130
containerTag: number,
@@ -856,10 +856,10 @@ function addFragmentHandleToInstance(
856856
fragmentInstance: FragmentInstanceType,
857857
): void {
858858
if (enableFragmentRefsInstanceHandles) {
859-
if (instance.unstable_reactFragments == null) {
860-
instance.unstable_reactFragments = new Set();
859+
if (instance.reactFragments == null) {
860+
instance.reactFragments = new Set();
861861
}
862-
instance.unstable_reactFragments.add(fragmentInstance);
862+
instance.reactFragments.add(fragmentInstance);
863863
}
864864
}
865865

@@ -924,8 +924,8 @@ export function deleteChildFromFragmentInstance(
924924
instance,
925925
): any): PublicInstanceWithFragmentHandles);
926926
if (enableFragmentRefsInstanceHandles) {
927-
if (publicInstance.unstable_reactFragments != null) {
928-
publicInstance.unstable_reactFragments.delete(fragmentInstance);
927+
if (publicInstance.reactFragments != null) {
928+
publicInstance.reactFragments.delete(fragmentInstance);
929929
}
930930
}
931931
}

0 commit comments

Comments
 (0)