Skip to content

Commit d6dcad6

Browse files
authored
useFormState: Only emit markers if postback state is provided (#27374)
This is an optimization where useFormState will only emit extra comment markers if a form state is passed at the root. If no state is passed, we don't need to emit anything because none of the hooks will match.
1 parent 54c2f2a commit d6dcad6

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,13 +2015,18 @@ function mountFormState<S, P>(
20152015
): [S, (P) => void] {
20162016
let initialState = initialStateProp;
20172017
if (getIsHydrating()) {
2018-
const isMatching = tryToClaimNextHydratableFormMarkerInstance(
2019-
currentlyRenderingFiber,
2020-
);
20212018
const root: FiberRoot = (getWorkInProgressRoot(): any);
20222019
const ssrFormState = root.formState;
2023-
if (ssrFormState !== null && isMatching) {
2024-
initialState = ssrFormState[0];
2020+
// If a formState option was passed to the root, there are form state
2021+
// markers that we need to hydrate. These indicate whether the form state
2022+
// matches this hook instance.
2023+
if (ssrFormState !== null) {
2024+
const isMatching = tryToClaimNextHydratableFormMarkerInstance(
2025+
currentlyRenderingFiber,
2026+
);
2027+
if (isMatching) {
2028+
initialState = ssrFormState[0];
2029+
}
20252030
}
20262031
}
20272032
const initialStateThenable: Thenable<S> = {

packages/react-server/src/ReactFizzServer.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,11 +1193,10 @@ function finishFunctionComponent(
11931193
formStateMatchingIndex: number,
11941194
) {
11951195
let didEmitFormStateMarkers = false;
1196-
if (formStateCount !== 0) {
1196+
if (formStateCount !== 0 && request.formState !== null) {
11971197
// For each useFormState hook, emit a marker that indicates whether we
1198-
// rendered using the form state passed at the root.
1199-
// TODO: As an optimization, Fizz should only emit these markers if form
1200-
// state is passed at the root.
1198+
// rendered using the form state passed at the root. We only emit these
1199+
// markers if form state is passed at the root.
12011200
const segment = task.blockedSegment;
12021201
if (segment === null) {
12031202
// Implies we're in reumable mode.

0 commit comments

Comments
 (0)