Repository navigation
Add missing docs for requestFormReset API #8576
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
Open
smg99
wants to merge
1
commit into
reactjs:main
Choose a base branch
from
smg99:docs/request-form-reset-reference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| --- | ||
| title: requestFormReset | ||
| --- | ||
|
|
||
| <Intro> | ||
|
|
||
| `requestFormReset` lets you reset a React-managed form after an Action or Transition finishes. | ||
|
|
||
| ```js | ||
| requestFormReset(form) | ||
| ``` | ||
|
|
||
| </Intro> | ||
|
|
||
| <InlineToc /> | ||
|
|
||
| --- | ||
|
|
||
| ## Reference {/*reference*/} | ||
|
|
||
| ### `requestFormReset(form)` {/*requestformreset*/} | ||
|
|
||
| Call `requestFormReset` to request that a form resets after the current Action or Transition completes. | ||
|
|
||
| ```js | ||
| import { startTransition } from 'react'; | ||
| import { requestFormReset } from 'react-dom'; | ||
|
|
||
| function handleSubmit(form) { | ||
| startTransition(async () => { | ||
| const formData = new FormData(form); | ||
| requestFormReset(form); | ||
| await save(formData); | ||
| }); | ||
| } | ||
| ``` | ||
|
|
||
| [See more examples below.](#usage) | ||
|
|
||
| #### Parameters {/*parameters*/} | ||
|
|
||
| * `form`: an [HTML form element](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement) rendered by React. | ||
|
|
||
| #### Returns {/*returns*/} | ||
|
|
||
| `requestFormReset` returns nothing. | ||
|
|
||
| #### Caveats {/*caveats*/} | ||
|
|
||
| * Pass a form element that is rendered by React. Passing a non-form element, or a form not managed by React, throws an error. | ||
| * `requestFormReset` is intended for Actions and Transitions. If called outside an Action or Transition, React warns in development and performs a synchronous reset. | ||
| * `requestFormReset` is useful when you implement form submission logic yourself (for example with `onSubmit` + `startTransition`) and still want React's form reset behavior. | ||
| * This API requests a reset for uncontrolled form fields. For controlled fields, handle resets by updating state, such as in `onReset`. | ||
|
|
||
| --- | ||
|
|
||
| ## Usage {/*usage*/} | ||
|
|
||
| ### Requesting a reset in a custom form Action {/*requesting-a-reset-in-a-custom-form-action*/} | ||
|
|
||
| React automatically resets uncontrolled fields after a successful `<form action={...}>` submission. When you implement submission manually, you can opt into the same behavior by calling `requestFormReset`. | ||
|
|
||
| ```js | ||
| import { startTransition } from 'react'; | ||
| import { requestFormReset } from 'react-dom'; | ||
|
|
||
| function SearchForm() { | ||
| async function onSubmit(event) { | ||
| event.preventDefault(); | ||
|
|
||
| const form = event.currentTarget; | ||
| const formData = new FormData(form); | ||
|
|
||
| startTransition(async () => { | ||
| requestFormReset(form); | ||
| await submitSearch(formData); | ||
| }); | ||
| } | ||
|
|
||
| return ( | ||
| <form onSubmit={onSubmit}> | ||
| <input name="query" defaultValue="react" /> | ||
| <button type="submit">Search</button> | ||
| </form> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| If your submission uses the built-in `<form action={...}>` behavior, you don't need to call `requestFormReset` manually. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have existing docs for the automatic behavior we can link to? May be worth a backlink from the docs for automatic behavior to manual behavior.