Skip to content

Commit

Permalink
Automatically reset forms after action finishes (#28804)
Browse files Browse the repository at this point in the history
This updates the behavior of form actions to automatically reset the
form's uncontrolled inputs after the action finishes.

This is a frequent feature request for people using actions and it
aligns the behavior of client-side form submissions more closely with
MPA form submissions.

It has no impact on controlled form inputs. It's the same as if you
called `form.reset()` manually, except React handles the timing of when
the reset happens, which is tricky/impossible to get exactly right in
userspace.

The reset shouldn't happen until the UI has updated with the result of
the action. So, resetting inside the action is too early.

Resetting in `useEffect` is better, but it's later than ideal because
any effects that run before it will observe the state of the form before
it's been reset.

It needs to happen in the mutation phase of the transition. More
specifically, after all the DOM mutations caused by the transition have
been applied. That way the `defaultValue` of the inputs are updated
before the values are reset. The idea is that the `defaultValue`
represents the current, canonical value sent by the server.

Note: this change has no effect on form submissions that aren't
triggered by an action.

DiffTrain build for [41950d1](41950d1)
  • Loading branch information
acdlite committed Apr 10, 2024
1 parent 05f6de5 commit 6d6562e
Show file tree
Hide file tree
Showing 18 changed files with 1,398 additions and 546 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dc6a7e01e1d2fa5eb4974f9bb66e9e8fb40f6ef8
41950d14a538aa7411b00b28bcd94ae95a45976e
41 changes: 36 additions & 5 deletions compiled/facebook-www/ReactART-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ if (__DEV__) {
return self;
}

var ReactVersion = "19.0.0-www-classic-877fd74a";
var ReactVersion = "19.0.0-www-classic-5df29fbc";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -586,6 +586,7 @@ if (__DEV__) {
var ScheduleRetry = StoreConsistency;
var ShouldSuspendCommit = Visibility;
var DidDefer = ContentReset;
var FormReset = Snapshot;
var LifecycleEffectMask =
Passive$1 | Update | Callback | Ref | Snapshot | StoreConsistency; // Union of all commit flags (flags with the lifetime of a particular commit)

Expand Down Expand Up @@ -655,7 +656,8 @@ if (__DEV__) {
ContentReset |
Ref |
Hydrating |
Visibility;
Visibility |
FormReset;
var LayoutMask = Update | Callback | Ref | Visibility; // TODO: Split into PassiveMountMask and PassiveUnmountMask

var PassiveMask = Passive$1 | Visibility | ChildDeletion; // Union of tags that don't get reset on clones.
Expand Down Expand Up @@ -9384,13 +9386,29 @@ if (__DEV__) {
var _dispatcher$useState = dispatcher.useState(),
maybeThenable = _dispatcher$useState[0];

var nextState;

if (typeof maybeThenable.then === "function") {
var thenable = maybeThenable;
return useThenable(thenable);
nextState = useThenable(thenable);
} else {
var status = maybeThenable;
return status;
nextState = status;
} // The "reset state" is an object. If it changes, that means something
// requested that we reset the form.

var _dispatcher$useState2 = dispatcher.useState(),
nextResetState = _dispatcher$useState2[0];

var prevResetState =
currentHook !== null ? currentHook.memoizedState : null;

if (prevResetState !== nextResetState) {
// Schedule a form reset
currentlyRenderingFiber$1.flags |= FormReset;
}

return nextState;
}
function bailoutHooks(current, workInProgress, lanes) {
workInProgress.updateQueue = current.updateQueue; // TODO: Don't need to reset the flags here, because they're reset in the
Expand Down Expand Up @@ -21259,7 +21277,7 @@ if (__DEV__) {
// Allows us to avoid traversing the return path to find the nearest Offscreen ancestor.

var offscreenSubtreeIsHidden = false;
var offscreenSubtreeWasHidden = false;
var offscreenSubtreeWasHidden = false; // Used to track if a form needs to be reset at the end of the mutation phase.
var PossiblyWeakSet = typeof WeakSet === "function" ? WeakSet : Set;
var nextEffect = null; // Used for Profiling builds to track updaters.

Expand Down Expand Up @@ -23591,6 +23609,19 @@ if (__DEV__) {
}
}
}

if (flags & FormReset) {
{
if (finishedWork.type !== "form") {
// Paranoid coding. In case we accidentally start using the
// FormReset bit for something else.
error(
"Unexpected host component type. Expected a form. This is a " +
"bug in React."
);
}
}
}
}

return;
Expand Down
41 changes: 36 additions & 5 deletions compiled/facebook-www/ReactART-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ if (__DEV__) {
return self;
}

var ReactVersion = "19.0.0-www-modern-bf3441a6";
var ReactVersion = "19.0.0-www-modern-2b9608d3";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -588,6 +588,7 @@ if (__DEV__) {
var ScheduleRetry = StoreConsistency;
var ShouldSuspendCommit = Visibility;
var DidDefer = ContentReset;
var FormReset = Snapshot;

var HostEffectMask =
/* */
Expand Down Expand Up @@ -655,7 +656,8 @@ if (__DEV__) {
ContentReset |
Ref |
Hydrating |
Visibility;
Visibility |
FormReset;
var LayoutMask = Update | Callback | Ref | Visibility; // TODO: Split into PassiveMountMask and PassiveUnmountMask

var PassiveMask = Passive$1 | Visibility | ChildDeletion; // Union of tags that don't get reset on clones.
Expand Down Expand Up @@ -9135,13 +9137,29 @@ if (__DEV__) {
var _dispatcher$useState = dispatcher.useState(),
maybeThenable = _dispatcher$useState[0];

var nextState;

if (typeof maybeThenable.then === "function") {
var thenable = maybeThenable;
return useThenable(thenable);
nextState = useThenable(thenable);
} else {
var status = maybeThenable;
return status;
nextState = status;
} // The "reset state" is an object. If it changes, that means something
// requested that we reset the form.

var _dispatcher$useState2 = dispatcher.useState(),
nextResetState = _dispatcher$useState2[0];

var prevResetState =
currentHook !== null ? currentHook.memoizedState : null;

if (prevResetState !== nextResetState) {
// Schedule a form reset
currentlyRenderingFiber$1.flags |= FormReset;
}

return nextState;
}
function bailoutHooks(current, workInProgress, lanes) {
workInProgress.updateQueue = current.updateQueue; // TODO: Don't need to reset the flags here, because they're reset in the
Expand Down Expand Up @@ -20632,7 +20650,7 @@ if (__DEV__) {
// Allows us to avoid traversing the return path to find the nearest Offscreen ancestor.

var offscreenSubtreeIsHidden = false;
var offscreenSubtreeWasHidden = false;
var offscreenSubtreeWasHidden = false; // Used to track if a form needs to be reset at the end of the mutation phase.
var PossiblyWeakSet = typeof WeakSet === "function" ? WeakSet : Set;
var nextEffect = null; // Used for Profiling builds to track updaters.

Expand Down Expand Up @@ -22958,6 +22976,19 @@ if (__DEV__) {
}
}
}

if (flags & FormReset) {
{
if (finishedWork.type !== "form") {
// Paranoid coding. In case we accidentally start using the
// FormReset bit for something else.
error(
"Unexpected host component type. Expected a form. This is a " +
"bug in React."
);
}
}
}
}

return;
Expand Down
20 changes: 13 additions & 7 deletions compiled/facebook-www/ReactART-prod.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2622,10 +2622,16 @@ function renderWithHooksAgain(workInProgress, Component, props, secondArg) {
return children;
}
function TransitionAwareHostComponent() {
var maybeThenable = ReactSharedInternals.H.useState()[0];
return "function" === typeof maybeThenable.then
? useThenable(maybeThenable)
: maybeThenable;
var dispatcher = ReactSharedInternals.H,
maybeThenable = dispatcher.useState()[0];
maybeThenable =
"function" === typeof maybeThenable.then
? useThenable(maybeThenable)
: maybeThenable;
dispatcher = dispatcher.useState()[0];
(null !== currentHook ? currentHook.memoizedState : null) !== dispatcher &&
(currentlyRenderingFiber$1.flags |= 1024);
return maybeThenable;
}
function bailoutHooks(current, workInProgress, lanes) {
workInProgress.updateQueue = current.updateQueue;
Expand Down Expand Up @@ -7855,7 +7861,7 @@ function recursivelyTraverseMutationEffects(root$jscomp$0, parentFiber) {
captureCommitPhaseError(childToDelete, parentFiber, error);
}
}
if (parentFiber.subtreeFlags & 12854)
if (parentFiber.subtreeFlags & 13878)
for (parentFiber = parentFiber.child; null !== parentFiber; )
commitMutationEffectsOnFiber(parentFiber, root$jscomp$0),
(parentFiber = parentFiber.sibling);
Expand Down Expand Up @@ -10599,7 +10605,7 @@ var slice = Array.prototype.slice,
return null;
},
bundleType: 0,
version: "19.0.0-www-classic-f1bbc587",
version: "19.0.0-www-classic-52a8955f",
rendererPackageName: "react-art"
};
var internals$jscomp$inline_1322 = {
Expand Down Expand Up @@ -10630,7 +10636,7 @@ var internals$jscomp$inline_1322 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "19.0.0-www-classic-f1bbc587"
reconcilerVersion: "19.0.0-www-classic-52a8955f"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1323 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
20 changes: 13 additions & 7 deletions compiled/facebook-www/ReactART-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -2420,10 +2420,16 @@ function renderWithHooksAgain(workInProgress, Component, props, secondArg) {
return children;
}
function TransitionAwareHostComponent() {
var maybeThenable = ReactSharedInternals.H.useState()[0];
return "function" === typeof maybeThenable.then
? useThenable(maybeThenable)
: maybeThenable;
var dispatcher = ReactSharedInternals.H,
maybeThenable = dispatcher.useState()[0];
maybeThenable =
"function" === typeof maybeThenable.then
? useThenable(maybeThenable)
: maybeThenable;
dispatcher = dispatcher.useState()[0];
(null !== currentHook ? currentHook.memoizedState : null) !== dispatcher &&
(currentlyRenderingFiber$1.flags |= 1024);
return maybeThenable;
}
function bailoutHooks(current, workInProgress, lanes) {
workInProgress.updateQueue = current.updateQueue;
Expand Down Expand Up @@ -7410,7 +7416,7 @@ function recursivelyTraverseMutationEffects(root$jscomp$0, parentFiber) {
captureCommitPhaseError(childToDelete, parentFiber, error);
}
}
if (parentFiber.subtreeFlags & 12854)
if (parentFiber.subtreeFlags & 13878)
for (parentFiber = parentFiber.child; null !== parentFiber; )
commitMutationEffectsOnFiber(parentFiber, root$jscomp$0),
(parentFiber = parentFiber.sibling);
Expand Down Expand Up @@ -10078,7 +10084,7 @@ var slice = Array.prototype.slice,
return null;
},
bundleType: 0,
version: "19.0.0-www-modern-4749dbb1",
version: "19.0.0-www-modern-e590ea90",
rendererPackageName: "react-art"
};
var internals$jscomp$inline_1307 = {
Expand Down Expand Up @@ -10109,7 +10115,7 @@ var internals$jscomp$inline_1307 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "19.0.0-www-modern-4749dbb1"
reconcilerVersion: "19.0.0-www-modern-e590ea90"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1308 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Loading

0 comments on commit 6d6562e

Please sign in to comment.