use-safe-action
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

usedSafeAction

It's just a repository to expose this implementation as a npm library. Thanks @AntonioErdeljac!

Creates a typed and self validated action to be used as hook in Nextjs Client Component.

How to use

First, create a Next Server Actions

'use server'

// import dependencies
import { type ActionState, createSafeAction } from "use-safe-action";
import { ContactFormInput } from "./schema";

type ReturnType = ActionState<ContactFormInput, string>

const handler = async (input: ContactFormInput): Promise<ReturnType> => {
    try {
        console.log(input)

        return {
            data: 'Success...'
        }

    } catch (error) {
        return {
            error: (error as Error).message
        }
    }
}

// export the action
export const sendContactEmail = createSafeAction(ContactFormInput, handler)
'use client'

import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { ContactFormInput } from "~/server/api/use-case/email/schema";
import { sendContactEmail } from "~/server/api/use-case/email/send-contact-email";
import { useToast } from "../ui/use-toast";
import { useAction } from "use-safe-action";

const ContactForm = () => {
    const { toast } = useToast();
    const form = useForm<ContactFormInput>({
        resolver: zodResolver(ContactFormInput)
    })
    // use the action
    const { execute, isLoading } = useAction(sendContactEmail, {
        onSuccess(data) {
            form.reset();

            toast({
                title: 'Sucess!',
                description: data,
                variant: 'success'
            })
        },
        onError(error) {
            toast({
                title: 'Error!',
                description: error,
                variant: 'destructive'
            })
        },
    });

    return ...;
}

export default ContactForm;

Package Sidebar

Install

npm i use-safe-action

Weekly Downloads

6

Version

1.0.1

License

MIT

Unpacked Size

72.7 kB

Total Files

14

Last publish

Collaborators

  • maykon-oliveira