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

@nephriteagain/react-hooks

v0.0.3

Published

A collection of React hooks for handling async operations with loading, error states, and data management

Downloads

3

Readme

@nephriteagain/react-hooks

A collection of useful React hooks for handling async operations.

Installation

npm install @nephriteagain/react-hooks

Hooks

useAsyncAction

A comprehensive hook for handling async actions with loading, error, and data states. Perfect for API calls that need full state management.

Usage

import { useAsyncAction } from '@nephriteagain/react-hooks';

function UserProfile() {
  const fetchUser = async (userId) => {
    const response = await fetch(`/api/users/${userId}`);
    return response.json();
  };

  const [runFetch, state] = useAsyncAction(fetchUser, {
    onComplete: () => console.log('User fetched!'),
    onError: (err) => console.error('Failed to fetch user:', err)
  });

  return (
    <div>
      <button onClick={() => runFetch(123)} disabled={state.isLoading}>
        {state.isLoading ? 'Loading...' : 'Fetch User'}
      </button>
      {state.isError && <p>Error: {state.error.message}</p>}
      {state.data && <p>User: {state.data.name}</p>}
    </div>
  );
}

API

useAsyncAction(fn, options?)

Parameters:

  • fn: The async function to wrap
  • options (optional):
    • onComplete: Callback executed when the async action completes successfully
    • onError: Callback executed when the async action fails

Returns: [run, state]

  • run: Function to execute the async action
  • state: Object containing:
    • data: The result data (null if not yet fetched or error occurred)
    • error: Error object (null if no error)
    • isLoading: Boolean indicating if the action is in progress
    • isError: Boolean indicating if an error occurred

useAsyncStatus

A simplified hook for tracking loading state of async functions. No error catching - only tracks loading state. Ideal for simple operations where you handle errors separately.

Usage

import { useAsyncStatus } from '@nephriteagain/react-hooks';

function SaveForm() {
  const saveData = async (formData) => {
    const response = await fetch('/api/save', {
      method: 'POST',
      body: JSON.stringify(formData)
    });
    return response.json();
  };

  const [runSave, isLoading] = useAsyncStatus(saveData);

  const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      await runSave({ name: 'John' });
      alert('Saved!');
    } catch (error) {
      alert('Error saving');
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <button type="submit" disabled={isLoading}>
        {isLoading ? 'Saving...' : 'Save'}
      </button>
    </form>
  );
}

API

useAsyncStatus(fn)

Parameters:

  • fn: The async function to wrap

Returns: [run, isLoading]

  • run: Function to execute the async action
  • isLoading: Boolean indicating if the action is in progress

useAsyncStateEffect

A hook for handling async operations that run automatically on mount and when dependencies change. Perfect for fetching data on component mount or when props/state values change.

Usage

import { useAsyncStateEffect } from '@nephriteagain/react-hooks';

function ProfilePage() {
  const fetchUserProfile = async () => {
    const response = await fetch('/api/profile');
    return response.json();
  };

  const [profile, { isLoading, isError, error, refresh, isRefreshing }] =
    useAsyncStateEffect(fetchUserProfile, [], {
      initialValue: null,
      loadingTrueAtStart: true
    });

  if (isLoading) return <div>Loading...</div>;
  if (isError) return <div>Error: {error.message}</div>;

  return (
    <div>
      <h1>{profile?.name}</h1>
      <button onClick={refresh} disabled={isRefreshing}>
        {isRefreshing ? 'Refreshing...' : 'Refresh'}
      </button>
    </div>
  );
}

// With dependencies - refetch when userId changes
function UserProfile({ userId }) {
  const [user, { isLoading, error }] = useAsyncStateEffect(
    async () => {
      const res = await fetch(`/api/users/${userId}`);
      return res.json();
    },
    [userId] // Refetch when userId changes
  );

  return <div>{user?.name}</div>;
}

API

useAsyncStateEffect(func, deps?, options?)

Parameters:

  • func: The async function to execute (should return a Promise)
  • deps (optional): Dependency array that triggers re-fetch when values change (default: [])
  • options (optional):
    • initialValue: Initial value before the first fetch completes
    • loadingTrueAtStart: Whether to set loading to true initially (default: true)

Returns: [value, state]

  • value: The fetched data
  • state: Object containing:
    • isLoading: Boolean indicating if the initial or dependency-triggered fetch is in progress
    • isError: Boolean indicating if an error occurred
    • error: Error object (null if no error)
    • refresh: Function to manually re-fetch the data
    • isRefreshing: Boolean indicating if a manual refresh is in progress

TypeScript Support

This package includes TypeScript type definitions. All hooks are fully typed with generics for type-safe usage.

import { useAsyncAction } from '@nephriteagain/react-hooks';

interface User {
  id: number;
  name: string;
}

const [fetchUser, state] = useAsyncAction<[number], User>(
  async (userId) => {
    const response = await fetch(`/api/users/${userId}`);
    return response.json();
  }
);

// state.data is typed as User | null

License

ISC