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

react-usefor-api

v1.0.10

Published

A lightweight React hook for handling API requests (GET, POST, PUT, DELETE) with loading and error states.

Readme

Reactjs useForm A lightweight React hook for handling API requests (GET, POST, PUT, DELETE) with loading and error states node version 20.^

Installation

  1. Install the package

Run this in your React project folder:

npm install @phaneth_pho/react-useform-api

or with Yarn:

yarn add @phaneth_pho/react-useform-api
  1. Import and use inside a React component

Example component (UserForm.jsx):

import React, { useEffect } from "react";
import useForm from "@phaneth_pho/react-useform-api";

export default function UserForm() {
  // Initialize the hook with your base API URL
  const token = localStorage.getItem("access_token");

  // noted token not required for register/login user
  const { data, errors, loading, get, post, put, del, reset } = useForm("https://api.yourapp.com", token);

  // Example: auto-fetch data on load
  useEffect(() => {
    get("/users");
  }, []);


 // Example: handle form submission (GET)
  const handeGetMethod = aysnc () =>{
    const res = await = get('/posts');
  }

  // Example: handle form submission (POST)
  const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      await post("/posts", {
        title: "Hello React Hook!",
        body: "This is a test POST request.",
        userId: 1,
      });
      alert("Data submitted successfully!");
    } catch (err) {
      console.error("API error:", errors);
    }
  };

  // Example: handle form submission (PUT)
  const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      await PUT(`/posts/${id}`, {
        title: "Hello React Hook!",
        body: "This is a test POST request.",
        userId: 1,
      });
      alert("Update Data successfully!");
    } catch (err) {
      console.error("API error:", errors);
    }
  };

  // Example: handle form delete (DELETE)
  const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      await DEL(`/posts/${id}`, id, true);
      alert("Delete Data successfully!");
    } catch (err) {
      console.error("API error:", errors);
    }
  };

  return (
    <div style={{ padding: 20 }}>
      <h2>User Form Example</h2>

      {loading && <p>⏳ Loading...</p>}

      <form onSubmit={handleSubmit}>
        <button type="submit">Submit</button>
      </form>

      {errors && (
        <p style={{ color: "red" }}>
          {errors.message || JSON.stringify(errors)}
        </p>
      )}

      {data && (
        <pre
          style={{
            background: "#f4f4f4",
            padding: 10,
            borderRadius: 8,
            marginTop: 10,
          }}
        >
          {JSON.stringify(data, null, 2)}
        </pre>
      )}
    </div>
  );
}
  1. Example API usage

You can now call any of these from your components:

Method Example Description

get(url)	get('/users')	#GET request
post(url, body)	post('/posts', {...})	#POST request
put(url, body)	put('/posts/1', {...})	#PUT request
del(url)	del('/posts/1', 1, true)	#DELETE request
reset()	reset()  #Clear form state
  1. Example for error handling

If the API returns a 400–500 error, the hook automatically stores it in errors. You can show it like this:

  {errors && <p className="error">{errors.message || "Something went wrong"}</p>}
  1. Example with Authorization token (optional)

If your API requires authentication, you can easily modify your useForm call:


const token = localStorage.getItem("access_token");
const { post } = useForm("https://api.yourapp.com", token);

await get("/users");
await post("/users", { name: "John Doe" });
await post(`/users/${id}`, { name: "John Doe" });
await del(`/users/${id}`, id, true); #base on your node express route req.body required if your app.use(express.json())

Or you can extend your package to accept a config object (I can show you that next if you’d like).

  1. Run your React app npm run dev #or npm start
npm run dev
npm run start

Now test your form — it should send real GET/POST/PUT/DELETE requests and manage loading + error states automatically