-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
c923257
commit 5f4238d
Showing
9 changed files
with
321 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use client' | ||
|
||
import { experimental_useFormState as useFormState } from 'react-dom' | ||
import { experimental_useFormStatus as useFormStatus } from 'react-dom' | ||
import { createTodo } from '@/app/actions' | ||
|
||
const initialState = { | ||
message: null, | ||
} | ||
|
||
function SubmitButton() { | ||
const { pending } = useFormStatus() | ||
|
||
return ( | ||
<button type="submit" aria-disabled={pending}> | ||
Add | ||
</button> | ||
) | ||
} | ||
|
||
export function AddForm() { | ||
const [state, formAction] = useFormState(createTodo, initialState) | ||
|
||
return ( | ||
<form action={formAction}> | ||
<label htmlFor="todo">Enter Task</label> | ||
<input type="text" id="todo" name="todo" required /> | ||
<SubmitButton /> | ||
<p aria-live="polite" className="sr-only" role="status"> | ||
{state?.message} | ||
</p> | ||
</form> | ||
) | ||
} |
Oops, something went wrong.