npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

next-form-handler

v1.0.1

Published

**next-form-handler** is a lightweight and flexible library for managing forms in **Next.js** applications. It simplifies form state management, validation, and handling of success and error events.

Readme

🚀 next-form-handler

next-form-handler is a lightweight and flexible library for managing forms in Next.js applications.
It simplifies form state management, validation, and handling of success and error events.


📦 Installation

✅ Install as an npm package

If you want to use next-form-handler in your Next.js or React project, install it via:

Using npm:

npm install next-form-handler

Using bun:

bun add next-form-handler

⚡ Usage

🔹 Use useFormHandler in a component

'use client';

import { createSubmitHandler, useFormHandler } from "next-form-handler";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { toast } from "sonner";

const schema = z.object({
    name: z.string()
});

export default function Form() {
    const { formAction, isPending } = useFormHandler(
        async (_, formData) => {
            // Simulating API request
            await new Promise((resolve) => setTimeout(resolve, 1000));
            return { success: true, message: "Form submitted successfully!" };
        },
        ({ message }) => {
            if (message) {
                toast.success(message);
            }
        },
        ({ error }) => {
            toast.error(error);
        }
    );

    const { handleSubmit, register } = useForm({
        resolver: zodResolver(schema),
    });

    const onSubmit = handleSubmit(createSubmitHandler(formAction));

    return (
        <form onSubmit={onSubmit}>
            <input type="text" {...register('name')} placeholder="Enter your name" />
            <button type="submit" disabled={isPending}>
                {isPending ? 'Loading...' : 'Send'}
            </button>
        </form>
    );
}

🎨 Install as a shadcn/ui component

If you're using shadcn/ui, you can install the FormWrapper component directly:

bunx shadcn@latest add https://raw.githubusercontent.com/Tayeq/next-form-handler/refs/heads/main/public/r/form-wrapper.json

This will add the FormWrapper component, which integrates seamlessly with next-form-handler and shadcn/ui styles.


🔥 Example Usage

form.tsx

'use client'

import {submitForm} from "./actions";
import {useForm} from "react-hook-form";
import {zodResolver} from "@hookform/resolvers/zod";
import {z} from "zod";
import {FormWrapper, useFormWrapper} from "@/components/form-wrapper";
import {FormControl, FormField, FormItem, FormLabel, FormMessage} from "@/components/ui/form";
import {Input} from "@/components/ui/input";
import {Button} from "@/components/ui/button";

const schema = z.object({
    name: z.string().min(3).max(255)
})

export default function Form() {
    const form = useForm({
        resolver: zodResolver(schema),
        defaultValues: {
            name: ''
        }
    })
    return (
        <FormWrapper action={submitForm} form={form} onSuccess={({data}) => {
            {
                form.reset(data)
                console.log(data)
            }
        }}>
            <FormField name={'name'} render={({field}) => (
                <FormItem>
                    <FormLabel>Name</FormLabel>
                    <FormControl>
                        <Input {...field}/>
                    </FormControl>
                    <FormMessage/>
                </FormItem>
            )}/>
            <SubmitButton/>
        </FormWrapper>
    )
}

const SubmitButton = () => {
    const {isPending} = useFormWrapper();
    return (
        <Button disabled={isPending} type={'submit'}>{isPending ? 'Loading' : 'Submit'}</Button>
    )
}

actions.ts

'use server'

import {FormAction} from "next-form-handler";

export const submitForm: FormAction<{
    name: string,
}> = async (state, formData) => {
    if(Math.random() > 0.5){
        return {
            success: false,
            error: 'Failed to submit form'
        }
    }

    const name = formData.get('name') as string;
    const upperCaseName = name.toUpperCase();

    return {
        success: true,
        message: 'Form submitted successfully',
        data: {
            name: upperCaseName
        }
    }
}

🛠 Features

✔️ Handles form state (success, error, validation)
✔️ Works with server actions in Next.js
✔️ Customizable success and error handlers
✔️ Supports different notification libraries
✔️ shadcn/ui integration


📜 License

MIT