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-api-client

v1.2.4

Published

A powerful and customizable rest api client for react

Downloads

36

Readme

react-api-client

NPM JavaScript Style Guide

Install

npm install --save react-api-client

Usage

Define a result type

import { BaseApiResult } from 'react-api-client'

interface ApiResult extends BaseApiResult {
  /* Add custom fields here */
}

Create an api client

import { ApiClient } from 'react-api-client'

const client = new ApiClient<ApiResult>({
  baseUrl: "http://your-server/api", // Your api base url
  responseHandler: (data) => { // Handle an api response
    if (data.error != null) {
      return {
        hasError: true,
        errorMessage: data.error,
        /* Extract additional error information from data here */
      }
    }

    return {
      hasError: false
    }
  },
  errorHandler: (e) => ({ // Handle an error while performing a request
    hasError: true,
    errorMessage: e.message
  }),
  fetchOptions: {
    /* Add extra options for the fetch() method here */
  }
});

Send a request from normal code

result = await client.get("/");
result = await client.post("/", {});

Send a request as a state

function MyComponent() {
  const [
    result,  /* The api result */
    loading, /* Indicates if the request is loading or not */
    reload   /* Call this function to perform the request again */
  ] = client.useGet("/");

  return (
    <span>{loading ? "Loading..." : JSON.stringify(result)}</span>
  );
}

Send a request with a state

function MyComponent() {
  const [
    handle,  /* The request state handle - pass this to all api calls */
    loading, /* Indicates if the request is loading or not */,
    result   /* The request result or null, if there was no result yet */
  ] = client.useRequestState();

  return (
    <div>
      <span>{loading ? "Loading..." : JSON.stringify(result)}</span><br />
      <button onClick={() => client.get("/", handle)}>Send request</button>
    </div>
  );
}

Send a request using a data loader (with inline handler)

function MyComponent() {
  return (
    <client.Loader consumer={true} endpoint="/">
      {(result: ApiResult, reload: ReloadFunction) => (
        <span>{JSON.stringify(result)}</span>
      )}
    </client.Loader>
  )
}

Send a request using a data loader (with context handler)

import { ReloadFunction } from 'react-api-client'

const DataContext = React.createContext("dataContext");

function MyOuterComponent() {
  return (
    <client.Loader consumer={DataContext} endpoint="/">
      <MyComponent />
    </client.Loader>
  )
}

function MyComponent() {
  const [
    result,  /* The api result */
    reload   /* Call this function to perform the request again */
  ] = useContext(DataContext);

  return (<span>{JSON.stringify(result)}</span>);
}

License

MIT © sinnlosername