You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/hooks/useFormState.md
+31-31Lines changed: 31 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -5,16 +5,16 @@ canary: true
5
5
6
6
<Canary>
7
7
8
-
The `useFormState` Hook is currently only available in React's canary and experimental channels. Learn more about [React's release channels here](/community/versioning-policy#all-release-channels). In addition, you need to use a framework that supports React Server Components to get the full benefit of `useFormState`.
8
+
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`.
9
9
10
10
</Canary>
11
11
12
12
<Intro>
13
13
14
-
`useFormState` is a Hook that allows you to read the return value of the form action after a form is submitted.
14
+
`useFormState` is a Hook that allows you to update state based on the result of a form action.
In the context of React Server Components, an *action* is a function that may be [executed when a form is submitted](/reference/react-dom/components/form). You can execute actions on the server or on the client.
{/* TODO T164397693: link to actions documentation once it exists */}
33
31
34
-
Call `useFormState` at the top level of your component to see the return value of an action after submitting a form. You pass `useFormState` an existing action as well as an initial state, and it returns a new action that you use when submitting your form, along with the latest form state.
32
+
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.
@@ -56,23 +57,22 @@ If used with a server action, `useFormState` allows the server's response from s
56
57
57
58
#### Parameters {/*parameters*/}
58
59
59
-
*`action`: The action to be performed when the form is submitted. When the action is called, it will receive the previous state of the form (initially the `initialState` that you pass, subsequently its previous return value) as its initial argument, followed by the arguments that an action normally receives.
60
-
*`initialState`: The value you want the state to be initially. It can be any serializable value. This argument is ignored after the form is first submitted.
60
+
*`fn`: The function to be called when the form is submitted or button pressed. When the function is called, it will receive the previous state of the form (initially the `initialState` that you pass, subsequently its previous return value) as its initial argument, followed by the arguments that a form action normally receives.
61
+
*`initialState`: The value you want the state to be initially. It can be any serializable value. This argument is ignored after the action is first invoked.
61
62
62
63
{/* TODO T164397693: link to serializable values docs once it exists */}
63
64
64
-
65
65
#### Returns {/*returns*/}
66
66
67
67
`useFormState` returns an array with exactly two values:
68
68
69
-
1. The current state. During the first render, it will match the `initialState` you have passed. After the form is submitted, it will match the value returned by the action.
70
-
2. A new action that you can pass as the `action` prop to your `form` component.
69
+
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.
70
+
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.
71
71
72
72
#### Caveats {/*caveats*/}
73
73
74
-
* 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, there is no advantage to using it over component local state.
75
-
* The action passed to `useFormState` receives an extra argument, the previous or initial state state, as its first argument. This makes its signature different than if it were used directly without `useFormState`.
74
+
* 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.
75
+
* 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`.
76
76
77
77
---
78
78
@@ -102,7 +102,7 @@ function MyComponent() {
102
102
1. The <CodeStepstep={1}>current state</CodeStep> of the form, which is initially set to the <CodeStepstep={4}>initial state</CodeStep> you provided, and after the form is submitted is set to the return value of the <CodeStepstep={3}>action</CodeStep> you provided.
103
103
2. A <CodeStepstep={2}>new action</CodeStep> that you pass to `<form>` as its `action` prop.
104
104
105
-
When the form is submitted, the <CodeStepstep={3}>action</CodeStep> that you provided will be called. Its return value will become the new <CodeStepstep={1}>current state</CodeStep> of the form.
105
+
When the form is submitted, the <CodeStepstep={3}>action</CodeStep> function that you provided will be called. Its return value will become the new <CodeStepstep={1}>current state</CodeStep> of the form.
106
106
107
107
The <CodeStepstep={3}>action</CodeStep> that you provide will also receive a new first argument, namely the <CodeStepstep={1}>current state</CodeStep> of the form. The first time the form is submitted, this will be the <CodeStepstep={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
108
108
@@ -141,8 +141,8 @@ function AddToCartForm({itemID, itemTitle}) {
141
141
exportdefaultfunctionApp() {
142
142
return (
143
143
<>
144
-
<AddToCartForm itemID="1" itemTitle="Javascript: The Definitive Guide"/>
145
-
<AddToCartForm itemID="2" itemTitle="Javascript: The Good Parts"/>
144
+
<AddToCartForm itemID="1" itemTitle="JavaScript: The Definitive Guide"/>
145
+
<AddToCartForm itemID="2" itemTitle="JavaScript: The Good Parts"/>
146
146
</>
147
147
)
148
148
}
@@ -176,8 +176,8 @@ form button {
176
176
```json package.json hidden
177
177
{
178
178
"dependencies": {
179
-
"react": "experimental",
180
-
"react-dom": "experimental",
179
+
"react": "canary",
180
+
"react-dom": "canary",
181
181
"react-scripts": "^5.0.0"
182
182
},
183
183
"main": "/index.js",
@@ -223,8 +223,8 @@ function AddToCartForm({itemID, itemTitle}) {
223
223
exportdefaultfunctionApp() {
224
224
return (
225
225
<>
226
-
<AddToCartForm itemID="1" itemTitle="Javascript: The Definitive Guide"/>
227
-
<AddToCartForm itemID="2" itemTitle="Javascript: The Good Parts"/>
226
+
<AddToCartForm itemID="1" itemTitle="JavaScript: The Definitive Guide"/>
227
+
<AddToCartForm itemID="2" itemTitle="JavaScript: The Good Parts"/>
0 commit comments