Files
Lee RobinsonandGitHub ec46f90739 docs: Use useFormState over useActionState (#69547)
We recently merged and combined the RC docs into the main docs, so there
is a single codebase. This added new icons and callouts for parts of
Next.js that are experimental or in a canary release.

However, this gets a little tricky with React 19. There have been
changes to `useFormState` and `useFormStatus` which differ from the
latest v14.x release and 15 + React 19 RC.

As 14 is still the stable release, we need to default to showing
`useFormState` and add proper callouts for those on canary/RC. I also
added more details to the upgrade guide for v15.
2024-09-01 22:16:38 -05:00

36 lines
803 B
TypeScript

"use client";
import { useActionState } from "react";
import { useFormStatus } from "react-dom";
import { createTodo } from "@/app/actions";
const initialState = {
message: "",
};
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" aria-disabled={pending}>
Add
</button>
);
}
export function AddForm() {
// useActionState is available with React 19 (Next.js App Router)
const [state, formAction] = useActionState(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>
);
}