-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Rename useFormState to useActionState #6776
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,14 +324,14 @@ Displaying a form submission error message before the JavaScript bundle loads fo | |
|
||
1. `<form>` be rendered by a [Server Component](/reference/react/use-client) | ||
1. the function passed to the `<form>`'s `action` prop be a [Server Action](/reference/react/use-server) | ||
1. the `useFormState` Hook be used to display the error message | ||
1. the `useActionState` Hook be used to display the error message | ||
|
||
`useFormState` takes two parameters: a [Server Action](/reference/react/use-server) and an initial state. `useFormState` returns two values, a state variable and an action. The action returned by `useFormState` should be passed to the `action` prop of the form. The state variable returned by `useFormState` can be used to displayed an error message. The value returned by the [Server Action](/reference/react/use-server) passed to `useFormState` will be used to update the state variable. | ||
`useActionState` takes two parameters: a [Server Action](/reference/react/use-server) and an initial state. `useActionState` returns two values, a state variable and an action. The action returned by `useActionState` should be passed to the `action` prop of the form. The state variable returned by `useActionState` can be used to displayed an error message. The value returned by the [Server Action](/reference/react/use-server) passed to `useActionState` will be used to update the state variable. | ||
|
||
<Sandpack> | ||
|
||
```js src/App.js | ||
import { useFormState } from "react-dom"; | ||
import { useActionState } from "react"; | ||
import { signUpNewUser } from "./api"; | ||
|
||
export default function Page() { | ||
|
@@ -345,12 +345,12 @@ export default function Page() { | |
return err.toString(); | ||
} | ||
} | ||
const [message, formAction] = useFormState(signup, null); | ||
const [message, signupAction] = useActionState(signup, null); | ||
return ( | ||
<> | ||
<h1>Signup for my newsletter</h1> | ||
<p>Signup with the same email twice to see an error</p> | ||
<form action={formAction} id="signup-form"> | ||
<form action={signupAction} id="signup-form"> | ||
<label htmlFor="email">Email: </label> | ||
<input name="email" id="email" placeholder="[email protected]" /> | ||
<button>Sign up</button> | ||
|
@@ -386,7 +386,7 @@ export async function signUpNewUser(newEmail) { | |
|
||
</Sandpack> | ||
|
||
Learn more about updating state from a form action with the [`useFormState`](/reference/react-dom/hooks/useFormState) docs | ||
Learn more about updating state from a form action with the [`useActionState`](/reference/react/hooks/useActionState) docs | ||
|
||
### Handling multiple submission types {/*handling-multiple-submission-types*/} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
--- | ||
title: useFormState | ||
title: useActionState | ||
canary: true | ||
--- | ||
|
||
<Canary> | ||
|
||
The `useFormState` Hook is currently only available in React's Canary and experimental channels. Learn more about [release channels here](/community/versioning-policy#all-release-channels). In addition, you need to use a framework that supports [React Server Components](/reference/react/use-client) to get the full benefit of `useFormState`. | ||
The `useActionState` Hook is currently only available in React's Canary and experimental channels. Learn more about [release channels here](/community/versioning-policy#all-release-channels). In addition, you need to use a framework that supports [React Server Components](/reference/react/use-client) to get the full benefit of `useActionState`. | ||
|
||
</Canary> | ||
|
||
<Intro> | ||
|
||
`useFormState` is a Hook that allows you to update state based on the result of a form action. | ||
`useActionState` is a Hook that allows you to update state based on the result of a form action. | ||
|
||
```js | ||
const [state, formAction] = useFormState(fn, initialState, permalink?); | ||
const [state, formAction] = useActionState(fn, initialState, permalink?); | ||
``` | ||
|
||
</Intro> | ||
|
@@ -25,21 +25,21 @@ const [state, formAction] = useFormState(fn, initialState, permalink?); | |
|
||
## Reference {/*reference*/} | ||
|
||
### `useFormState(action, initialState, permalink?)` {/*useformstate*/} | ||
### `useActionState(action, initialState, permalink?)` {/*useactionstate*/} | ||
|
||
{/* TODO T164397693: link to actions documentation once it exists */} | ||
|
||
Call `useFormState` at the top level of your component to create component state that is updated [when a form action is invoked](/reference/react-dom/components/form). You pass `useFormState` an existing form action function as well as an initial state, and it returns a new action that you use in your form, along with the latest form state. The latest form state is also passed to the function that you provided. | ||
Call `useActionState` at the top level of your component to create component state that is updated [when a form action is invoked](/reference/react-dom/components/form). You pass `useActionState` an existing form action function as well as an initial state, and it returns a new action that you use in your form, along with the latest form state. The latest form state is also passed to the function that you provided. | ||
|
||
```js | ||
import { useFormState } from "react-dom"; | ||
import { useActionState } from "react"; | ||
|
||
async function increment(previousState, formData) { | ||
return previousState + 1; | ||
} | ||
|
||
function StatefulForm({}) { | ||
const [state, formAction] = useFormState(increment, 0); | ||
const [state, formAction] = useActionState(increment, 0); | ||
return ( | ||
<form> | ||
{state} | ||
|
@@ -51,7 +51,7 @@ function StatefulForm({}) { | |
|
||
The form state is the value returned by the action when the form was last submitted. If the form has not yet been submitted, it is the initial state that you pass. | ||
|
||
If used with a Server Action, `useFormState` allows the server's response from submitting the form to be shown even before hydration has completed. | ||
If used with a Server Action, `useActionState` allows the server's response from submitting the form to be shown even before hydration has completed. | ||
|
||
[See more examples below.](#usage) | ||
|
||
|
@@ -65,30 +65,30 @@ If used with a Server Action, `useFormState` allows the server's response from s | |
|
||
#### Returns {/*returns*/} | ||
|
||
`useFormState` returns an array with exactly two values: | ||
`useActionState` returns an array with exactly two values: | ||
|
||
1. The current state. During the first render, it will match the `initialState` you have passed. After the action is invoked, it will match the value returned by the action. | ||
2. A new action that you can pass as the `action` prop to your `form` component or `formAction` prop to any `button` component within the form. | ||
|
||
#### Caveats {/*caveats*/} | ||
|
||
* When used with a framework that supports React Server Components, `useFormState` lets you make forms interactive before JavaScript has executed on the client. When used without Server Components, it is equivalent to component local state. | ||
* The function passed to `useFormState` receives an extra argument, the previous or initial state, as its first argument. This makes its signature different than if it were used directly as a form action without using `useFormState`. | ||
* When used with a framework that supports React Server Components, `useActionState` lets you make forms interactive before JavaScript has executed on the client. When used without Server Components, it is equivalent to component local state. | ||
* The function passed to `useActionState` receives an extra argument, the previous or initial state, as its first argument. This makes its signature different than if it were used directly as a form action without using `useActionState`. | ||
|
||
--- | ||
|
||
## Usage {/*usage*/} | ||
|
||
### Using information returned by a form action {/*using-information-returned-by-a-form-action*/} | ||
|
||
Call `useFormState` at the top level of your component to access the return value of an action from the last time a form was submitted. | ||
Call `useActionState` at the top level of your component to access the return value of an action from the last time a form was submitted. | ||
|
||
```js [[1, 5, "state"], [2, 5, "formAction"], [3, 5, "action"], [4, 5, "null"], [2, 8, "formAction"]] | ||
import { useFormState } from 'react-dom'; | ||
import { useActionState } from 'react'; | ||
import { action } from './actions.js'; | ||
|
||
function MyComponent() { | ||
const [state, formAction] = useFormState(action, null); | ||
const [state, formAction] = useActionState(action, null); | ||
// ... | ||
return ( | ||
<form action={formAction}> | ||
|
@@ -98,14 +98,14 @@ function MyComponent() { | |
} | ||
``` | ||
|
||
`useFormState` returns an array with exactly two items: | ||
`useActionState` returns an array with exactly two items: | ||
|
||
1. The <CodeStep step={1}>current state</CodeStep> of the form, which is initially set to the <CodeStep step={4}>initial state</CodeStep> you provided, and after the form is submitted is set to the return value of the <CodeStep step={3}>action</CodeStep> you provided. | ||
2. A <CodeStep step={2}>new action</CodeStep> that you pass to `<form>` as its `action` prop. | ||
|
||
When the form is submitted, the <CodeStep step={3}>action</CodeStep> function that you provided will be called. Its return value will become the new <CodeStep step={1}>current state</CodeStep> of the form. | ||
|
||
The <CodeStep step={3}>action</CodeStep> that you provide will also receive a new first argument, namely the <CodeStep step={1}>current state</CodeStep> of the form. The first time the form is submitted, this will be the <CodeStep step={4}>initial state</CodeStep> you provided, while with subsequent submissions, it will be the return value from the last time the action was called. The rest of the arguments are the same as if `useFormState` had not been used. | ||
The <CodeStep step={3}>action</CodeStep> that you provide will also receive a new first argument, namely the <CodeStep step={1}>current state</CodeStep> of the form. The first time the form is submitted, this will be the <CodeStep step={4}>initial state</CodeStep> you provided, while with subsequent submissions, it will be the return value from the last time the action was called. The rest of the arguments are the same as if `useActionState` had not been used. | ||
|
||
```js [[3, 1, "action"], [1, 1, "currentState"]] | ||
function action(currentState, formData) { | ||
|
@@ -118,17 +118,16 @@ function action(currentState, formData) { | |
|
||
#### Display form errors {/*display-form-errors*/} | ||
|
||
To display messages such as an error message or toast that's returned by a Server Action, wrap the action in a call to `useFormState`. | ||
To display messages such as an error message or toast that's returned by a Server Action, wrap the action in a call to `useActionState`. | ||
|
||
<Sandpack> | ||
|
||
```js src/App.js | ||
import { useState } from "react"; | ||
import { useFormState } from "react-dom"; | ||
import { useActionState, useState } from "react"; | ||
import { addToCart } from "./actions.js"; | ||
|
||
function AddToCartForm({itemID, itemTitle}) { | ||
const [message, formAction] = useFormState(addToCart, null); | ||
const [message, formAction] = useActionState(addToCart, null); | ||
return ( | ||
<form action={formAction}> | ||
<h2>{itemTitle}</h2> | ||
|
@@ -196,12 +195,11 @@ The return value from a Server Action can be any serializable value. For example | |
<Sandpack> | ||
|
||
```js src/App.js | ||
import { useState } from "react"; | ||
import { useFormState } from "react-dom"; | ||
import { useActionState, useState } from "react"; | ||
import { addToCart } from "./actions.js"; | ||
|
||
function AddToCartForm({itemID, itemTitle}) { | ||
const [formState, formAction] = useFormState(addToCart, {}); | ||
const [formState, formAction] = useActionState(addToCart, {}); | ||
return ( | ||
<form action={formAction}> | ||
<h2>{itemTitle}</h2> | ||
|
@@ -283,7 +281,7 @@ form button { | |
|
||
### My action can no longer read the submitted form data {/*my-action-can-no-longer-read-the-submitted-form-data*/} | ||
|
||
When you wrap an action with `useFormState`, it gets an extra argument *as its first argument*. The submitted form data is therefore its *second* argument instead of its first as it would usually be. The new first argument that gets added is the current state of the form. | ||
When you wrap an action with `useActionState`, it gets an extra argument *as its first argument*. The submitted form data is therefore its *second* argument instead of its first as it would usually be. The new first argument that gets added is the current state of the form. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A lot is referring to forms. Probably needs another pass to abstract this more from DOM forms specifically and explain how to use actions w/o forms. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, fwiw I was waiting to rewrite this before moving it here for that reason |
||
|
||
```js | ||
function action(currentState, formData) { | ||
|
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.
Should there be a section
Action Hooks
maybe? Not sure where this should go.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.
Yeah I'd create a section with useTransition, useActionState, and useDeferredValue and call it like "UI hooks" or something