-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useFormState and useActionState integration #64
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
🦋 Changeset detectedLatest commit: 39dad2f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This also addresses #29 |
Thanks for this! One small piece of feedback: |
new proposal, fixes #70 The change adds a type: "state" to inputs in actions.ts: "use server"
import z from "zod"
import { createServerAction } from "zsa"
export const produceNewMessage = createServerAction()
.input(
z.object({
name: z.string().min(5),
}),
{
type: "state",
}
)
.handler(async ({ input }) => {
await new Promise((resolve) => setTimeout(resolve, 500))
return "Hello, " + input.name
}) in client component: "use client"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { useActionState } from "react"
import { produceNewMessage } from "./actions"
export default function UseActionStateExample() {
const [[data, err], submitAction, isPending] = useActionState(
produceNewMessage,
[null, null] // or [initialData, null]
)
return (
<Card className="not-prose">
<CardHeader>
<CardTitle>Use Action State</CardTitle>
</CardHeader>
<CardContent className="flex flex-col gap-4">
<form action={submitAction} className="flex flex-col gap-4">
<Input name="name" placeholder="Enter your name..." />
<Button>Create message</Button>
</form>
{isPending ? (
<div>loading...</div>
) : data ? (
<div>{data}</div>
) : err ? (
<div>error : {JSON.stringify(err)}</div>
) : null}
</CardContent>
</Card>
)
} |
In response to #63
This PR introduces
createActionStateHookFrom
that takes in eitheruseActionState
oruseFormState
and makes it super easy to usezsa
actions.