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
I am working with Next.js 15.1 and React 19. In my project, I have a PostForm component for creating and updating posts. I want to know whether I should use useStateAction or useAction.
Additionally, could you help me identify if there are any unnecessary fields or missing fields in the code below?
post-form.tsx
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "./ui/input";
import { Textarea } from "./ui/textarea";
import { PostInfer, postSchema } from "@/types/post-schema";
import { useStateAction } from "next-safe-action/stateful-hooks";
import { upsertPost } from "@/server/action/post";
import { Post } from "@prisma/client";
import { toast } from "sonner";
import { useRouter } from "next/navigation";
export function PostForm({ post }: { post?: Post }) {
const router = useRouter();
const form = useForm<PostInfer>({
resolver: zodResolver(postSchema),
defaultValues: {
title: post?.title || "",
content: post?.content || "",
},
});
const { execute, result, status } = useStateAction(upsertPost, {
initResult: {
data: { success: false, message: "" },
},
onSuccess: (result) => {
toast.success(result.data?.message || "Post created successfully");
if (post?.id) {
router.push("/");
} else {
form.reset();
}
},
onError: () => {
toast.error(
result.data?.message || "An error occurred while saving the post"
);
},
});
async function onSubmit(values: PostInfer) {
await execute({
...values,
id: post?.id,
});
}
return (
<Card className="w-full max-w-md mx-auto mt-10">
<CardHeader>
<CardTitle>Create New Post</CardTitle>
</CardHeader>
<CardContent>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>Title</FormLabel>
<FormControl>
<Input placeholder="Enter post title" {...field} />
</FormControl>
<FormDescription>
This is the title of your blog post.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="content"
render={({ field }) => (
<FormItem>
<FormLabel>Content</FormLabel>
<FormControl>
<Textarea
placeholder="Enter post content"
className="resize-none"
{...field}
/>
</FormControl>
<FormDescription>
Write your blog post content here.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" disabled={status === "executing"}>
{status === "executing" ? "Submitting..." : "Submit"}
</Button>
</form>
</Form>
</CardContent>
</Card>
);
}
import { z } from "zod";
export const postSchema = z.object({
id: z.string().optional(),
title: z.string().min(2, {
message: "Title must be at least 2 characters.",
}),
content: z.string().min(2, {
message: "Content must be at least 2 characters.",
}),
});
export type PostInfer = z.infer<typeof postSchema>;
export type PostSchemaType = z.infer<typeof postSchema>;
safe-action.ts
import { createSafeActionClient } from "next-safe-action";
import { z } from "zod";
export const actionClient = createSafeActionClient({
defineMetadataSchema() {
return z.object({
actionName: z.string(),
});
},
});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am working with Next.js 15.1 and React 19. In my project, I have a PostForm component for creating and updating posts. I want to know whether I should use useStateAction or useAction.
Additionally, could you help me identify if there are any unnecessary fields or missing fields in the code below?
post-form.tsx
upsertPost.ts
post-schema.ts
safe-action.ts
Beta Was this translation helpful? Give feedback.
All reactions