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

Automatically reset forms after action finishes #28804

Merged
merged 1 commit into from
Apr 10, 2024

Commits on Apr 10, 2024

  1. Automatically reset forms after action finishes

    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.
    acdlite committed Apr 10, 2024
    Configuration menu
    Copy the full SHA
    562f9c9 View commit details
    Browse the repository at this point in the history