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
This PR shows how to use a new React hook `useFormState` in the context
of the [Forms and
Mutations](https://nextjs.org/docs/app/building-your-application/data-fetching/forms-and-mutations)
docs. It also updates the forms example (`next-forms`) to show the
recommended patterns for loading / error states.
Related: #55399
---
Co-authored-by: John Pham <johnphammail@gmail.com>
You can use [`usePathname()`](/docs/app/api-reference/functions/use-pathname) to determine if a link is active. For example, to add a class to the active link, you can check if the current `pathname` matches the `href` of the link:
@@ -360,14 +360,16 @@ export default async function submit(formData) {
360
360
361
361
<AppOnly>
362
362
363
-
Use the `useFormStatus` hook to show a loading state when a form is submitting on the server:
363
+
Use the `useFormStatus` hook to show a loading state when a form is submitting on the server. The `useFormStatus` hook can only be used as a child of a `form` element using a Server Action.
> - Displaying loading or error states currently requires using Client Components. We are exploring options for server-side functions to retrieve these values as we move forward in stability for Server Actions.
395
+
`<SubmitButton />` can then be used in a form with a Server Action:
396
+
397
+
```tsx filename="app/page.tsx" switcher
398
+
import { SubmitButton } from'@/app/submit-button'
399
+
400
+
exportdefaultasyncfunction Home() {
401
+
return (
402
+
<formaction={...}>
403
+
<inputtype="text"name="field-name" />
404
+
<SubmitButton />
405
+
</form>
406
+
)
407
+
}
408
+
```
409
+
410
+
```jsx filename="app/page.jsx" switcher
411
+
import { SubmitButton } from'@/app/submit-button'
412
+
413
+
exportdefaultasyncfunctionHome() {
414
+
return (
415
+
<form action={...}>
416
+
<input type="text" name="field-name"/>
417
+
<SubmitButton />
418
+
</form>
419
+
)
420
+
}
421
+
```
422
+
423
+
<details>
424
+
<summary>Examples</summary>
425
+
426
+
-[Form with Loading & Error States](https://github.com/vercel/next.js/tree/canary/examples/next-forms)
427
+
428
+
</details>
396
429
397
430
</AppOnly>
398
431
@@ -484,89 +517,116 @@ export default function Page() {
484
517
485
518
<AppOnly>
486
519
487
-
Server Actions can also return [serializable objects](https://developer.mozilla.org/docs/Glossary/Serialization). For example, your Server Action might handle errors from creating a new item, returning either a success or error message:
520
+
Server Actions can also return [serializable objects](https://developer.mozilla.org/docs/Glossary/Serialization). For example, your Server Action might handle errors from creating a new item:
Then, from a Client Component, you can read this value and save it to state, allowing the component to display the result of the Server Action to the viewer.
548
+
Then, from a Client Component, you can read this value and display an error message.
> - Displaying loading or error states currently requires using Client Components. We are exploring options for server-side functions to retrieve these values as we move forward in stability for Server Actions.
624
+
<details>
625
+
<summary>Examples</summary>
626
+
627
+
- [Form with Loading & Error States](https://github.com/vercel/next.js/tree/canary/examples/next-forms)
0 commit comments