Skip to content

Commit

Permalink
Docs: Add note about useActionState and useFormState React versio…
Browse files Browse the repository at this point in the history
…ns (#68880)

Warn users about React versions for `useFormState` vs. `useActionState`.

- `useFormState` was renamed to `useActionState` in React 19 RC.
- There are no React docs for `useFormState` that we can link to but the
`useActionState` mentions the change.

To avoid having to update all mentions of `useActionState` now, then
update all mentions of `useFormState` in the future, I've kept the
`useActionState` examples as is, and added a callout about the React
versions in the pages that have `useActionState` examples.
  • Loading branch information
delbaoliveira authored Aug 14, 2024
1 parent 3e0bf6b commit ada7d2f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ related:

Errors can be divided into two categories: **expected errors** and **uncaught exceptions**:

- **Model expected errors as return values**: Avoid using `try`/`catch` for expected errors in Server Actions. Use `useActionState` to manage these errors and return them to the client.
- **Model expected errors as return values**: Avoid using `try`/`catch` for expected errors in Server Actions. Use [`useActionState`](https://react.dev/reference/react/useActionState) to manage these errors and return them to the client.
- **Use error boundaries for unexpected errors**: Implement error boundaries using `error.tsx` and `global-error.tsx` files to handle unexpected errors and provide a fallback UI.

> **Good to know**: These examples use React's `useActionState` hook, which is available in React 19 RC. If you are using an earlier version of React, use `useFormState` instead. See the [React docs](https://react.dev/reference/react/useActionState) for more information.
## Handling Expected Errors

Expected errors are those that can occur during the normal operation of the application, such as those from [server-side form validation](/docs/app/building-your-application/data-fetching/server-actions-and-mutations#server-side-form-validation) or failed requests. These errors should be handled explicitly and returned to the client.

### Handling Expected Errors from Server Actions

Use the `useActionState` hook (previously `useFormState`) to manage the state of Server Actions, including handling errors. This approach avoids `try`/`catch` blocks for expected errors, which should be modeled as return values rather than thrown exceptions.
Use the [`useActionState`](https://react.dev/reference/react/useActionState) hook to manage the state of Server Actions, including handling errors. This approach avoids `try`/`catch` blocks for expected errors, which should be modeled as return values rather than thrown exceptions.

```tsx filename="app/actions.ts" switcher
'use server'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ This will trigger the submission of the nearest `<form>` ancestor, which will in
### Server-side form validation
We recommend using HTML validation like `required` and `type="email"` for basic client-side form validation.
You can use the HTML attributes like `required` and `type="email"` for basic client-side form validation.
For more advanced server-side validation, you can use a library like [zod](https://zod.dev/) to validate the form fields before mutating the data:
Expand Down Expand Up @@ -351,6 +351,8 @@ Once the fields have been validated on the server, you can return a serializable
- By passing the action to `useActionState`, the action's function signature changes to receive a new `prevState` or `initialState` parameter as its first argument.
- `useActionState` is a React hook and therefore must be used in a Client Component.
> **Good to know**: These examples use React's `useActionState` hook, which is available in React 19 RC. If you are using an earlier version of React, use `useFormState` instead. See the [React docs](https://react.dev/reference/react/useActionState) for more information.
```tsx filename="app/actions.ts" switcher
'use server'

Expand Down Expand Up @@ -440,7 +442,6 @@ export function Signup() {
> **Good to know:**
>
> - Before mutating data, you should always ensure a user is also authorized to perform the action. See [Authentication and Authorization](#authentication-and-authorization).
> - In earlier React Canary versions, this API was part of React DOM and called `useFormState`.
### Pending states
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ The examples on this page walk through basic username and password auth for educ

### Sign-up and login functionality

You can use the [`<form>`](https://react.dev/reference/react-dom/components/form) element with React's [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and [`useActionState()`](https://react.dev/reference/react/useActionState) to capture user credentials, validate form fields, and call your Authentication Provider's API or database.
> **Good to know**: These examples use React's `useActionState` hook, which is available in React 19 RC. If you are using an earlier version of React, use `useFormState` instead. See the [React docs](https://react.dev/reference/react/useActionState) for more information.
You can use the [`<form>`](https://react.dev/reference/react-dom/components/form) element with React's [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and [`useActionState`](https://react.dev/reference/react/useActionState) to capture user credentials, validate form fields, and call your Authentication Provider's API or database.

Since Server Actions always execute on the server, they provide a secure environment for handling authentication logic.

Expand Down

0 comments on commit ada7d2f

Please sign in to comment.