Created
December 10, 2024 16:55
-
-
Save mmikhan/7030324bf07def97bdde3074f5172831 to your computer and use it in GitHub Desktop.
A simpel React form with server component (RSC) and Action State
This file contains hidden or 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
'use server' | |
export async function signUpAction() { | |
const { data, error } = await authClient.signUp.email({ | |
email: "[email protected]", | |
password: "password", | |
name: "John Doe", | |
}); | |
return { data, error }; | |
} |
This file contains hidden or 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
"use client"; | |
import { startTransition, useActionState } from "react"; | |
import { signUpAction } from "@/app/(auth)/sign-up/_lib/action"; | |
const initialState = { | |
success: false, | |
message: "", | |
data: null, | |
error: null, | |
}; | |
export default function SignInPage() { | |
const [state, action, isPending] = useActionState(signUpAction, initialState); | |
return ( | |
<form | |
action={async (formData: FormData) => { | |
startTransition(() => { | |
action(); | |
}); | |
}} | |
> | |
<input type="email" name="email" placeholder="Email" /> | |
<input type="password" name="password" placeholder="Password" /> | |
<button type="submit" disabled={isPending}> | |
{isPending ? "Signing in..." : "Sign In"} | |
</button> | |
{state.error && <div className="text-red-500">{state.error.message}</div>} | |
</form> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment