Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useFormState: Only emit markers if postback state is provided #27374

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 10 additions & 5 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2015,13 +2015,18 @@ function mountFormState<S, P>(
): [S, (P) => void] {
let initialState = initialStateProp;
if (getIsHydrating()) {
const isMatching = tryToClaimNextHydratableFormMarkerInstance(
currentlyRenderingFiber,
);
const root: FiberRoot = (getWorkInProgressRoot(): any);
const ssrFormState = root.formState;
if (ssrFormState !== null && isMatching) {
initialState = ssrFormState[0];
// If a formState option was passed to the root, there are form state
// markers that we need to hydrate. These indicate whether the form state
// matches this hook instance.
if (ssrFormState !== null) {
const isMatching = tryToClaimNextHydratableFormMarkerInstance(
currentlyRenderingFiber,
);
if (isMatching) {
initialState = ssrFormState[0];
}
}
}
const initialStateThenable: Thenable<S> = {
Expand Down
7 changes: 3 additions & 4 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1193,11 +1193,10 @@ function finishFunctionComponent(
formStateMatchingIndex: number,
) {
let didEmitFormStateMarkers = false;
if (formStateCount !== 0) {
if (formStateCount !== 0 && request.formState !== null) {
// For each useFormState hook, emit a marker that indicates whether we
// rendered using the form state passed at the root.
// TODO: As an optimization, Fizz should only emit these markers if form
// state is passed at the root.
// rendered using the form state passed at the root. We only emit these
// markers if form state is passed at the root.
const segment = task.blockedSegment;
if (segment === null) {
// Implies we're in reumable mode.
Expand Down