Skip to content

Commit 4f55010

Browse files
authored
Rename useFormState to useActionState (#6776)
* move useFormState api * add redirect * use React DOM as the name
1 parent 9fb2f0d commit 4f55010

File tree

8 files changed

+53
-45
lines changed

8 files changed

+53
-45
lines changed

src/content/blog/2024/02/15/react-labs-what-we-have-been-working-on-february-2024.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ We refer to this broader collection of features as simply "Actions". Actions all
5252
</form>
5353
```
5454

55-
The `action` function can operate synchronously or asynchronously. You can define them on the client side using standard JavaScript or on the server with the [`'use server'`](/reference/react/use-server) directive. When using an action, React will manage the life cycle of the data submission for you, providing hooks like [`useFormStatus`](/reference/react-dom/hooks/useFormStatus), and [`useFormState`](/reference/react-dom/hooks/useFormState) to access the current state and response of the form action.
55+
The `action` function can operate synchronously or asynchronously. You can define them on the client side using standard JavaScript or on the server with the [`'use server'`](/reference/react/use-server) directive. When using an action, React will manage the life cycle of the data submission for you, providing hooks like [`useFormStatus`](/reference/react-dom/hooks/useFormStatus), and [`useActionState`](/reference/react/useActionState) to access the current state and response of the form action.
5656

5757
By default, Actions are submitted within a [transition](/reference/react/useTransition), keeping the current page interactive while the action is processing. Since Actions support async functions, we've also added the ability to use `async/await` in transitions. This allows you to show pending UI with the `isPending` state of a transition when an async request like `fetch` starts, and show the pending UI all the way through the update being applied.
5858

@@ -78,7 +78,7 @@ React Server Components, Asset Loading, Document Metadata, and Actions have all
7878

7979
- **Asset Loading**: we integrated Suspense with the loading lifecycle of resources such as stylesheets, fonts, and scripts so that React takes them into account to determine whether the content in elements like [`<style>`](/reference/react-dom/components/style), [`<link>`](/reference/react-dom/components/link), and [`<script>`](/reference/react-dom/components/script) are ready to be displayed. We’ve also added new [Resource Loading APIs](/reference/react-dom#resource-preloading-apis) like `preload` and `preinit` to provide greater control for when a resource should load and initialize.
8080

81-
- **Actions**: As shared above, we've added Actions to manage sending data from the client to the server. You can add `action` to elements like [`<form/>`](/reference/react-dom/components/form), access the status with [`useFormStatus`](/reference/react-dom/hooks/useFormStatus), handle the result with [`useFormState`](/reference/react-dom/hooks/useFormState), and optimistically update the UI with [`useOptimistic`](/reference/react/useOptimistic).
81+
- **Actions**: As shared above, we've added Actions to manage sending data from the client to the server. You can add `action` to elements like [`<form/>`](/reference/react-dom/components/form), access the status with [`useFormStatus`](/reference/react-dom/hooks/useFormStatus), handle the result with [`useActionState`](/reference/react/useActionState), and optimistically update the UI with [`useOptimistic`](/reference/react/useOptimistic).
8282

8383
Since all of these features work together, it’s difficult to release them in the Stable channel individually. Releasing Actions without the complementary hooks for accessing form states would limit the practical usability of Actions. Introducing React Server Components without integrating Server Actions would complicate modifying data on the server.
8484

src/content/reference/react-dom/components/form.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,14 @@ Displaying a form submission error message before the JavaScript bundle loads fo
324324

325325
1. `<form>` be rendered by a [Server Component](/reference/react/use-client)
326326
1. the function passed to the `<form>`'s `action` prop be a [Server Action](/reference/react/use-server)
327-
1. the `useFormState` Hook be used to display the error message
327+
1. the `useActionState` Hook be used to display the error message
328328

329-
`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.
329+
`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.
330330

331331
<Sandpack>
332332

333333
```js src/App.js
334-
import { useFormState } from "react-dom";
334+
import { useActionState } from "react";
335335
import { signUpNewUser } from "./api";
336336

337337
export default function Page() {
@@ -345,12 +345,12 @@ export default function Page() {
345345
return err.toString();
346346
}
347347
}
348-
const [message, formAction] = useFormState(signup, null);
348+
const [message, signupAction] = useActionState(signup, null);
349349
return (
350350
<>
351351
<h1>Signup for my newsletter</h1>
352352
<p>Signup with the same email twice to see an error</p>
353-
<form action={formAction} id="signup-form">
353+
<form action={signupAction} id="signup-form">
354354
<label htmlFor="email">Email: </label>
355355
<input name="email" id="email" placeholder="[email protected]" />
356356
<button>Sign up</button>
@@ -386,7 +386,7 @@ export async function signUpNewUser(newEmail) {
386386

387387
</Sandpack>
388388

389-
Learn more about updating state from a form action with the [`useFormState`](/reference/react-dom/hooks/useFormState) docs
389+
Learn more about updating state from a form action with the [`useActionState`](/reference/react/hooks/useActionState) docs
390390

391391
### Handling multiple submission types {/*handling-multiple-submission-types*/}
392392

src/content/reference/react-dom/hooks/index.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ Form Hooks are currently only available in React's canary and experimental chann
2121
*Forms* let you create interactive controls for submitting information. To manage forms in your components, use one of these Hooks:
2222

2323
* [`useFormStatus`](/reference/react-dom/hooks/useFormStatus) allows you to make updates to the UI based on the status of the a form.
24-
* [`useFormState`](/reference/react-dom/hooks/useFormState) allows you to manage state inside a form.
2524

2625
```js
2726
function Form({ action }) {
2827
async function increment(n) {
2928
return n + 1;
3029
}
31-
const [count, incrementFormAction] = useFormState(increment, 0);
30+
const [count, incrementFormAction] = useActionState(increment, 0);
3231
return (
3332
<form action={action}>
3433
<button formAction={incrementFormAction}>Count: {count}</button>
@@ -46,4 +45,3 @@ function Button() {
4645
);
4746
}
4847
```
49-

src/content/reference/react/hooks.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ These Hooks are mostly useful to library authors and aren't commonly used in the
113113
- [`useDebugValue`](/reference/react/useDebugValue) lets you customize the label React DevTools displays for your custom Hook.
114114
- [`useId`](/reference/react/useId) lets a component associate a unique ID with itself. Typically used with accessibility APIs.
115115
- [`useSyncExternalStore`](/reference/react/useSyncExternalStore) lets a component subscribe to an external store.
116+
* [`useActionState`](/reference/react/useActionState) allows you to manage state of actions.
116117
117118
---
118119

src/content/reference/react/use-server.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ By passing a Server Action to the form `action`, React can [progressively enhanc
133133

134134
In the username request form, there might be the chance that a username is not available. `requestUsername` should tell us if it fails or not.
135135

136-
To update the UI based on the result of a Server Action while supporting progressive enhancement, use [`useFormState`](/reference/react-dom/hooks/useFormState).
136+
To update the UI based on the result of a Server Action while supporting progressive enhancement, use [`useActionState`](/reference/react/useActionState).
137137

138138
```js
139139
// requestUsername.js
@@ -153,11 +153,11 @@ export default async function requestUsername(formData) {
153153
// UsernameForm.js
154154
'use client';
155155

156-
import { useFormState } from 'react-dom';
156+
import { useActionState } from 'react';
157157
import requestUsername from './requestUsername';
158158

159159
function UsernameForm() {
160-
const [returnValue, action] = useFormState(requestUsername, 'n/a');
160+
const [returnValue, action] = useActionState(requestUsername, 'n/a');
161161

162162
return (
163163
<>
@@ -171,7 +171,7 @@ function UsernameForm() {
171171
}
172172
```
173173

174-
Note that like most Hooks, `useFormState` can only be called in <CodeStep step={1}>[client code](/reference/react/use-client)</CodeStep>.
174+
Note that like most Hooks, `useActionState` can only be called in <CodeStep step={1}>[client code](/reference/react/use-client)</CodeStep>.
175175

176176
### Calling a Server Action outside of `<form>` {/*calling-a-server-action-outside-of-form*/}
177177

src/content/reference/react-dom/hooks/useFormState.md renamed to src/content/reference/react/useActionState.md

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
---
2-
title: useFormState
2+
title: useActionState
33
canary: true
44
---
55

66
<Canary>
77

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`.
8+
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`.
99

1010
</Canary>
1111

12+
<Note>
13+
14+
In earlier React Canary versions, this API was part of React DOM and called `useFormState`.
15+
16+
</Note>
17+
1218
<Intro>
1319

14-
`useFormState` is a Hook that allows you to update state based on the result of a form action.
20+
`useActionState` is a Hook that allows you to update state based on the result of a form action.
1521

1622
```js
17-
const [state, formAction] = useFormState(fn, initialState, permalink?);
23+
const [state, formAction] = useActionState(fn, initialState, permalink?);
1824
```
1925
2026
</Intro>
@@ -25,21 +31,21 @@ const [state, formAction] = useFormState(fn, initialState, permalink?);
2531
2632
## Reference {/*reference*/}
2733
28-
### `useFormState(action, initialState, permalink?)` {/*useformstate*/}
34+
### `useActionState(action, initialState, permalink?)` {/*useactionstate*/}
2935
3036
{/* TODO T164397693: link to actions documentation once it exists */}
3137
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.
38+
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.
3339
3440
```js
35-
import { useFormState } from "react-dom";
41+
import { useActionState } from "react";
3642

3743
async function increment(previousState, formData) {
3844
return previousState + 1;
3945
}
4046

4147
function StatefulForm({}) {
42-
const [state, formAction] = useFormState(increment, 0);
48+
const [state, formAction] = useActionState(increment, 0);
4349
return (
4450
<form>
4551
{state}
@@ -51,7 +57,7 @@ function StatefulForm({}) {
5157
5258
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.
5359
54-
If used with a Server Action, `useFormState` allows the server's response from submitting the form to be shown even before hydration has completed.
60+
If used with a Server Action, `useActionState` allows the server's response from submitting the form to be shown even before hydration has completed.
5561
5662
[See more examples below.](#usage)
5763
@@ -65,30 +71,30 @@ If used with a Server Action, `useFormState` allows the server's response from s
6571
6672
#### Returns {/*returns*/}
6773
68-
`useFormState` returns an array with exactly two values:
74+
`useActionState` returns an array with exactly two values:
6975
7076
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.
7177
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.
7278
7379
#### Caveats {/*caveats*/}
7480
75-
* 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.
76-
* 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`.
81+
* 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.
82+
* 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`.
7783
7884
---
7985
8086
## Usage {/*usage*/}
8187
8288
### Using information returned by a form action {/*using-information-returned-by-a-form-action*/}
8389
84-
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.
90+
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.
8591
8692
```js [[1, 5, "state"], [2, 5, "formAction"], [3, 5, "action"], [4, 5, "null"], [2, 8, "formAction"]]
87-
import { useFormState } from 'react-dom';
93+
import { useActionState } from 'react';
8894
import { action } from './actions.js';
8995

9096
function MyComponent() {
91-
const [state, formAction] = useFormState(action, null);
97+
const [state, formAction] = useActionState(action, null);
9298
// ...
9399
return (
94100
<form action={formAction}>
@@ -98,14 +104,14 @@ function MyComponent() {
98104
}
99105
```
100106
101-
`useFormState` returns an array with exactly two items:
107+
`useActionState` returns an array with exactly two items:
102108
103109
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.
104110
2. A <CodeStep step={2}>new action</CodeStep> that you pass to `<form>` as its `action` prop.
105111
106112
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.
107113
108-
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.
114+
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.
109115
110116
```js [[3, 1, "action"], [1, 1, "currentState"]]
111117
function action(currentState, formData) {
@@ -118,17 +124,16 @@ function action(currentState, formData) {
118124
119125
#### Display form errors {/*display-form-errors*/}
120126
121-
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`.
127+
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`.
122128
123129
<Sandpack>
124130
125131
```js src/App.js
126-
import { useState } from "react";
127-
import { useFormState } from "react-dom";
132+
import { useActionState, useState } from "react";
128133
import { addToCart } from "./actions.js";
129134

130135
function AddToCartForm({itemID, itemTitle}) {
131-
const [message, formAction] = useFormState(addToCart, null);
136+
const [message, formAction] = useActionState(addToCart, null);
132137
return (
133138
<form action={formAction}>
134139
<h2>{itemTitle}</h2>
@@ -196,12 +201,11 @@ The return value from a Server Action can be any serializable value. For example
196201
<Sandpack>
197202
198203
```js src/App.js
199-
import { useState } from "react";
200-
import { useFormState } from "react-dom";
204+
import { useActionState, useState } from "react";
201205
import { addToCart } from "./actions.js";
202206

203207
function AddToCartForm({itemID, itemTitle}) {
204-
const [formState, formAction] = useFormState(addToCart, {});
208+
const [formState, formAction] = useActionState(addToCart, {});
205209
return (
206210
<form action={formAction}>
207211
<h2>{itemTitle}</h2>
@@ -283,7 +287,7 @@ form button {
283287
284288
### My action can no longer read the submitted form data {/*my-action-can-no-longer-read-the-submitted-form-data*/}
285289
286-
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.
290+
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.
287291
288292
```js
289293
function action(currentState, formData) {

src/sidebarReference.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"title": "Hooks",
1515
"path": "/reference/react/hooks",
1616
"routes": [
17+
{
18+
"title": "useActionState",
19+
"path": "/reference/react/useActionState",
20+
"canary": true
21+
},
1722
{
1823
"title": "useCallback",
1924
"path": "/reference/react/useCallback"
@@ -174,11 +179,6 @@
174179
"title": "Hooks",
175180
"path": "/reference/react-dom/hooks",
176181
"routes": [
177-
{
178-
"title": "useFormState",
179-
"path": "/reference/react-dom/hooks/useFormState",
180-
"canary": true
181-
},
182182
{
183183
"title": "useFormStatus",
184184
"path": "/reference/react-dom/hooks/useFormStatus",

vercel.json

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"destination": "/reference/react",
1010
"permanent": true
1111
},
12+
{
13+
"source": "/reference/react-dom/hooks/useFormState",
14+
"destination": "/reference/react/useActionState",
15+
"permanent": true
16+
},
1217
{
1318
"source": "/learn/meet-the-team",
1419
"destination": "/community/team",

0 commit comments

Comments
 (0)