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

@saphe/react-use

v0.2.1

Published

A collection of tiny, useful, type-safe React hooks.

Readme

@saphe/react-use

NPM version NPM downloads License Bundle size Dependencies Code coverage Pull requests welcome

A collection of tiny, useful, type-safe React hooks.

Features

  • ⚖️ Incredibly lightweight, only ~150 LOC per hook (including types),
  • 🌳 Tree-shakable, only include in the bundle what is necessary,
  • 👍 All hooks have a sophisticated type system powering them,
  • ✔️ 100% test coverage,
  • 0️⃣ 0 dependencies.

Table of Contents

Getting Started

Install

pnpm add @saphe/react-use
# or
yarn add @saphe/react-use
# or
npm install @saphe/react-use

useAsyncReducer

The useAsyncReducer hook is a variation of React's core useReducer hook. Not a fork, but heavily inspired by @bitovi/use-simple-reducer.

useAsyncReducer takes an initial state object and an object of actions that would modify the state, and returns an object containing the current state, the actions that may be performed on the state, a boolean isLoading indicating if an action is processing, and an error object for if one of the actions threw an error. All of this with full type-safety.

Actions

Actions are sync or async functions that accept a state and additional parameters, and return the next state.

import { useAsyncReducer } from '@saphe/react-use';
import { ReactElement } from 'react';

export default function IncrementButtons(): ReactElement {
  const { state, actions } = useAsyncReducer(
    { count: 0 },
    {
      increment: (state) => ({ count: state.count + 1 }),
      add: async (state, n: number) => ({ count: state.count + n }),
    },
  );

  return (
    <>
      <button onClick={actions.increment}>increment</button>
      <button onClick={() => actions.add(5)}>add 5</button>
    </>
  );
}

Even though add was defined as an async function, it is called as a sync function with the predefined parameters (all type-safe). You can add as many parameters as you'd like and they can be of any type.

Queue

If async functions are called after each other, they are added to a queue and are executed one after another. During this execution, the isLoading boolean will be true.

Error handling

Any errors thrown within actions are caught and returned using the error object, which is null as long as there hasn't been an error.

const { error } = useAsyncReducer(
  { count: 0 },
  {
    increment: (state) => ({ count: state.count + 1 }),
    error: () => {
      throw Error('Something went horribly wrong');
    },
  },
);

If an error has occurred, the error object looks like this:

interface Error<State> {
  message: string;
  action: Action<State>;
  pendingActions: Action<State>[];
  runFailedAction: () => void;
  runPendingActions: () => void;
  runAllActions: () => void;
}

useUrlParams

The useUrlParams hook provides a type-safe way to parse your URL parameters. It takes a query object and either a parameter name, a list of parameter names, or a list of parameter configurations, and returns a type-safe object containing parsed values from the supplied query object.

Parameter config

There are three ways to configure a parameter.

  1. Define a single parameter name, returning an object with that parameter of type string | null:
const params = useUrlParams('singleStringParam');
params; // { singleStringParam: string | null; }
  1. Define a list of parameter names, returning an object with those parameters of type string | null:
const params = useUrlParams(['firstStringParam', 'secondStringParam']);
params; // { firstStringParam: string | null; secondStringParam: string | null; }
  1. Define a list of parameter configurations, returning an object with those parameters with their related type.
interface ParamConfig {
  name: string;
  /** Default: 'string' */
  type?: 'string' | 'number' | 'boolean';
  /** Default: false */
  array?: boolean;
}
const params = useUrlParams([
  { name: 'numberParam', type: 'number' },
  { name: 'numberArr', type: 'number', array: true },
  { name: 'boolParam', type: 'boolean' },
  { name: 'boolArr', type: 'boolean', array: true },
  { name: 'stringParam', type: 'string' },
  { name: 'stringArr', type: 'string', array: true },
]);
params; // { numberParam: number | null; numberArr: number[]; boolParam: boolean | null; ...etc }

Options 2 and 3 may be combined in a mixed configuration:

const params = useUrlParams(['first', { name: 'second', type: 'number' }]);
params; // { first: string | null; second: number | null; }

Next.JS integration

import { useUrlParams, UseUrlParamsTypes } from '@saphe/react-use';
import { useRouter } from 'next/router';

export default function useNextUrlParams<
  S extends string, 
  C extends UseUrlParamsTypes.Config<S>
>(
  config: S | C[],
  options?: UseUrlParamsTypes.Options,
): UseUrlParamsTypes.State<S, typeof config> {
  const router = useRouter();
  return useUrlParams(config, router.query, options);
}