Server Actions Example #10757
Replies: 5 comments 15 replies
-
In the React docs, it says that the function passed to |
Beta Was this translation helpful? Give feedback.
-
Why isn't it possible to pass the server action to the |
Beta Was this translation helpful? Give feedback.
-
EDIT: Nevermind figured it out, I forgot actions need to be in a separate file. "use client";
import { useTransition } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
type SignupData = {
username: string;
emailAddress: string;
password: string;
inviteCode: string;
};
export default function SignupForm() {
const [isPending, startTransition] = useTransition();
const { register, handleSubmit } = useForm<SignupData>();
const onSubmit: SubmitHandler<SignupData> = (data) => {
startTransition(async () => {
await new Promise((res) => setTimeout(res, 1000));
console.log("test");
});
};
return (
<>
<button>{JSON.stringify(isPending)}</button>
<form className="space-y-4" onSubmit={handleSubmit(onSubmit)}>
<div className="grid lg:grid-cols-2 space-x-4">
<div className="form-control">
<label className="label" htmlFor="username">
<span className="label-text">Username</span>
</label>
<input
className="input input-bordered"
id="username"
{...register("username")}
></input>
</div>
<div className="form-control">
<label className="label" htmlFor="email">
<span className="label-text">Email address</span>
</label>
<input
className="input input-bordered"
type="email"
id="email"
{...register("emailAddress")}
></input>
</div>
</div>
<div className="form-control">
<label className="label" htmlFor="password">
<span className="label-text">Password</span>
</label>
<input
className="input input-bordered"
type="password"
id="password"
{...register("password")}
></input>
</div>
<div className="form-control">
<label className="label" htmlFor="inviteCode">
<span className="label-text">Invite code</span>
</label>
<input
className="input input-bordered"
id="inviteCode"
{...register("inviteCode")}
></input>
</div>
<button className="btn btn-block btn-primary" type="submit">
Create account
</button>
</form>
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
I am still confuse why we need the useTransition hook on the server action outside the form action, try both it all work, however if i use the transition hook it break the RHF isSubmitting method, even isPending return false before the data is validated, and without transition the method isSubmitting is working as expected. EDIT: ok I manage to make isPending from the transition hook work by writing like this, but still, I still don't see what's the difference. const isMutating = isPending || isSubmitting
const onSubmitUpload = handleSubmit(async(data) => {
startTransition(async() => {
await uploadProject(data);
router.refresh();
})
}) |
Beta Was this translation helpful? Give feedback.
-
Here is another take on using Server Actions with Validators AJV & Zod (see branches): https://github.com/robahtou/issues-react-hook-form The main points are:
|
Beta Was this translation helpful? Give feedback.
-
Hi, I'm using react-hook-form with Next.js server actions. There has been some discussion here about how to do so, but I wanted to start this discussion to gather best practices. Here is my current implementation. Would appreciate any feedback to guide others who are doing the same!
Some context on this component: it's a simple form to create an item (name and description). After creating the item, it redirects to the table containing all the items
Beta Was this translation helpful? Give feedback.
All reactions