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-safe-form

v1.1.7

Published

Simplify React 19 Server Actions forms in Next.js

Readme

🚀 next-safe-form

The easiest way to handle forms with React 19 Server Actions in Next.js — clean, safe, and scalable.


✨ Why this library?

Using useActionState with forms in React 19 + Next.js can quickly become:

  • repetitive
  • inconsistent
  • hard to scale

You often have to manually handle:

  • loading states
  • field validation errors
  • server errors
  • form state syncing
  • side effects (toast, redirect, etc.)

👉 next-safe-form eliminates all this boilerplate.


⚡ Features

  • ✅ Clean abstraction over useActionState

  • ✅ Built for Next.js Server Actions

  • ✅ Automatic handling of:

    • loading state
    • success state
    • field errors
    • server errors
  • ✅ Type-safe

  • ✅ Minimal and intuitive API

  • ✅ Scalable architecture (client + server)


📦 Installation

npm install next-safe-form

or

pnpm add next-safe-form

or

yarn add next-safe-form

or

bun add next-safe-form

🧠 Core Idea

Instead of writing this over and over:

  • useActionState
  • error parsing
  • state handling
  • side effects

You just write:

useSafeActionForm();

🚀 Usage


1️⃣ Server Side (Recommended)

Use createSafeAction to standardize your server logic.

"use server";

import { createSafeAction } from "next-safe-form";
import { z } from "zod";

const schema = z.object({
  email: z.string().email(),
  password: z.string().min(6),
});

export const loginAction = createSafeAction({
  schema,
  handler: async (data) => {
    // business logic
    if (data.email !== "[email protected]") {
      throw new Error("Invalid credentials");
    }

    return {
      email: "",
      password: "",
    };
  },
});

2️⃣ Client Side

"use client";

import { useSafeActionForm } from "next-safe-form";

export default function LoginForm() {
  const { formAction, isPending, fieldsErrors, serverError } = useSafeActionForm({
    action: loginAction,
    initialValues: {
      email: "",
      password: "",
    },
    onSuccess: () => {
      alert("Login successful!");
    },
    onError: (err) => {
      alert(err);
    },
  });

  return (
    <form action={formAction}>
      <input name="email" placeholder="Email" />
      {fieldsErrors?.email && <p>{fieldsErrors.email}</p>}

      <input name="password" type="password" placeholder="Password" />

      {serverError && <p>{serverError}</p>}

      <button disabled={isPending}>{isPending ? "Loading..." : "Login"}</button>
    </form>
  );
}

🧩 API

useSafeActionForm(options)

| Option | Type | Description | | --------------- | -------- | ---------------------- | | action | function | Server action | | initialValues | object | Initial form values | | onSuccess | function | Called on success | | onError | function | Called on server error |


🔁 Returned values

| Property | Description | | -------------- | ----------------------- | | formAction | Form action handler | | isPending | Loading state | | fieldsErrors | Field validation errors | | serverError | Server error message | | data | Returned data | | isSuccess | Success state | | reset | Reset form |


createSafeAction(options)

| Option | Type | Description | | --------- | --------------------- | ---------------------- | | schema | Zod schema (optional) | Validation schema | | handler | function | Business logic handler |


🧠 Error Handling Model

All actions follow a standardized format:

{
  success: boolean,
  data: T,
  error?: {
    type: "fields" | "server",
    message?: string,
    fields?: Record<string, string>
  }
}

🏗️ Roadmap

  • [ ] Zod auto-inference (types)
  • [ ] Client-side validation
  • [ ] Form helpers (input binding)
  • [ ] Devtools
  • [ ] Toast integration

🤝 Contributing

Contributions are welcome!


📄 License

MIT


👤 Author

Built with ❤️ for Next.js developers by 4develhoper