Skip to content

Commit 1678530

Browse files
authored
fix hydration warning suppression in text comparisons (#24784)
* fix hydration warning suppression in text comparisons * lint * lowercase test
1 parent 652e6c5 commit 1678530

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4146,6 +4146,54 @@ describe('ReactDOMFizzServer', () => {
41464146
);
41474147
});
41484148

4149+
it('hydration warnings for mismatched text with multiple text nodes caused by suspending should be suppressed', async () => {
4150+
let resolve;
4151+
const Lazy = React.lazy(() => {
4152+
return new Promise(r => {
4153+
resolve = r;
4154+
});
4155+
});
4156+
4157+
function App({isClient}) {
4158+
return (
4159+
<div>
4160+
{isClient ? <Lazy /> : <p>lazy</p>}
4161+
<p>some {'text'}</p>
4162+
</div>
4163+
);
4164+
}
4165+
await act(async () => {
4166+
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />);
4167+
pipe(writable);
4168+
});
4169+
4170+
const errors = [];
4171+
ReactDOMClient.hydrateRoot(container, <App isClient={true} />, {
4172+
onRecoverableError(error) {
4173+
errors.push(error.message);
4174+
},
4175+
});
4176+
4177+
expect(Scheduler).toFlushAndYield([]);
4178+
expect(errors).toEqual([]);
4179+
expect(getVisibleChildren(container)).toEqual(
4180+
<div>
4181+
<p>lazy</p>
4182+
<p>some {'text'}</p>
4183+
</div>,
4184+
);
4185+
4186+
resolve({default: () => <p>lazy</p>});
4187+
expect(Scheduler).toFlushAndYield([]);
4188+
expect(errors).toEqual([]);
4189+
expect(getVisibleChildren(container)).toEqual(
4190+
<div>
4191+
<p>lazy</p>
4192+
<p>some {'text'}</p>
4193+
</div>,
4194+
);
4195+
});
4196+
41494197
describe('text separators', () => {
41504198
// To force performWork to start before resolving AsyncText but before piping we need to wait until
41514199
// after scheduleWork which currently uses setImmediate to delay performWork

packages/react-dom/src/client/ReactDOMHostConfig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,8 @@ export function didNotMatchHydratedContainerTextInstance(
971971
textInstance: TextInstance,
972972
text: string,
973973
isConcurrentMode: boolean,
974+
shouldWarnDev: boolean,
974975
) {
975-
const shouldWarnDev = true;
976976
checkForUnmatchedText(
977977
textInstance.nodeValue,
978978
text,
@@ -988,9 +988,9 @@ export function didNotMatchHydratedTextInstance(
988988
textInstance: TextInstance,
989989
text: string,
990990
isConcurrentMode: boolean,
991+
shouldWarnDev: boolean,
991992
) {
992993
if (parentProps[SUPPRESS_HYDRATION_WARNING] !== true) {
993-
const shouldWarnDev = true;
994994
checkForUnmatchedText(
995995
textInstance.nodeValue,
996996
text,

packages/react-reconciler/src/ReactFiberHydrationContext.new.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ function prepareToHydrateHostTextInstance(fiber: Fiber): boolean {
508508
textContent,
509509
// TODO: Delete this argument when we remove the legacy root API.
510510
isConcurrentMode,
511+
shouldWarnIfMismatchDev,
511512
);
512513
break;
513514
}
@@ -525,6 +526,7 @@ function prepareToHydrateHostTextInstance(fiber: Fiber): boolean {
525526
textContent,
526527
// TODO: Delete this argument when we remove the legacy root API.
527528
isConcurrentMode,
529+
shouldWarnIfMismatchDev,
528530
);
529531
break;
530532
}

packages/react-reconciler/src/ReactFiberHydrationContext.old.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ function prepareToHydrateHostTextInstance(fiber: Fiber): boolean {
508508
textContent,
509509
// TODO: Delete this argument when we remove the legacy root API.
510510
isConcurrentMode,
511+
shouldWarnIfMismatchDev,
511512
);
512513
break;
513514
}
@@ -525,6 +526,7 @@ function prepareToHydrateHostTextInstance(fiber: Fiber): boolean {
525526
textContent,
526527
// TODO: Delete this argument when we remove the legacy root API.
527528
isConcurrentMode,
529+
shouldWarnIfMismatchDev,
528530
);
529531
break;
530532
}

0 commit comments

Comments
 (0)