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-use-fetch-api

v2.1.1

Published

React hook for making simple JSON API requests using the browser Fetch API

Readme

react-use-fetch-api

An npm package that provides a React hook for making simple JSON API requests using the browser Fetch API

Usage

The useApi React hook gives you access to 5 functions, get, post, put, patch, and del. Simply import the hook into your component, and invoke it to return what you need.

A common pattern is to destructure the return value, naming only which function you'll need in your component.

Simple use cases

GET

import React, { useState } from 'react'
import { useApi } from 'react-use-fetch-api'

export default function InterestingComponent() {
  const { get } = useApi()
  const [data, setData] = useState({})

  useEffect(() => {
    get('https://jsonplaceholder.typicode.com/todos/1').then(data => {
      setData(data)
    })
  }, [])

  return (
    // render your interesting component here
  )
}

POST

import React from 'react'
import { useApi } from 'react-use-fetch-api'

export default function InterestingComponent() {
  const { post } = useApi()

  const onSubmit = () => {
    const newTodo = {
    userId: 1,
    title: "Gotta do all the things!",
    completed: false
  }

    post('https://jsonplaceholder.typicode.com/todos', newTodo).then(data => {
      // do something here like redirect the user or show a message or something
    })
  }

  return (
    // render your interesting component here
  )
}

PUT

import React from 'react'
import { useApi } from 'react-use-fetch-api'

export default function InterestingComponent() {
  const { put } = useApi()

  const onSubmit = () => {
    const updatedTodo = {
      userId: 1,
      title: "Gotta do all the things!",
      completed: false
    }

    put('https://jsonplaceholder.typicode.com/todos/1', updatedTodo).then(data => {
      // do something here like redirect the user or show a message or something
    })
  }

  return (
    // render your interesting component here
  )
}

PATCH

import React from 'react'
import { useApi } from 'react-use-fetch-api'

export default function InterestingComponent() {
  const { patch } = useApi()

  const onSubmit = () => {
    const updatedTodo = {
      userId: 1,
      title: "Gotta do all the things!",
      completed: false
    }

    patch('https://jsonplaceholder.typicode.com/todos/1', updatedTodo).then(data => {
      // do something here like redirect the user or show a message or something
    })
  }

  return (
    // render your interesting component here
  )
}

DEL

import React from 'react'
import { useApi } from 'react-use-fetch-api'

export default function InterestingComponent() {
  const { del } = useApi()

  const onDelete = () => {
    del('https://jsonplaceholder.typicode.com/todos/1').then(data => {
      // do something here like redirect the user or show a message or something
    })
  }

  return (
    // render your interesting component here
  )
}

Custom headers

All functions accept data and headers as optional parameters. If you don't pass custom headers, the default will be:

{
  "Content-Type": "application/json",
  Accept: "application/json"
}

To use custom headers:

const customHeaders = { "Content-Language": "de-DE" };

get("https://jsonplaceholder.typicode.com/todos/1", customHeaders);

post("https://jsonplaceholder.typicode.com/todos/1", newTodo, customHeaders);

put("https://jsonplaceholder.typicode.com/todos/1", updatedTodo, customHeaders);

patch("https://jsonplaceholder.typicode.com/todos/1", updatedTodo, customHeaders);

del("https://jsonplaceholder.typicode.com/todos/1", customHeaders);

To use custom headers with no data:

const customHeaders = { "Content-Language": "de-DE" };

get("https://jsonplaceholder.typicode.com/todos/1", customHeaders);

post("https://jsonplaceholder.typicode.com/todos/1", null, customHeaders);

put("https://jsonplaceholder.typicode.com/todos/1", null, customHeaders);

patch("https://jsonplaceholder.typicode.com/todos/1", null, customHeaders);

del("https://jsonplaceholder.typicode.com/todos/1", customHeaders);

Error handling

The useApi hook also allows you to gracefully handle error states.

It can take an onUnauthorized and/or an onError custom error handling function.

If the API response comes back with HTTP status code 401 (Unauthorized), the useApi hook will invoke your onAuthorized handler if provided.

For all other errors, the onError handler will be invoked if provided.

import React, { useState } from "react";
import { useApi } from "react-use-fetch-api";

export default function InterestingComponent() {
  // you could also be fancy and create your own
  // custom hooks for error handling 😉
  function onUnauthorized(response) {
    // handle the error here: remove a stale token,
    // present a message to the user, etc.
  }

  function onError(response) {
    // handle the error here: present a message to the user,
    // redirect them to a static page, etc.
  }

  const { get } = useApi(onUnauthorized, onError);

  // ... remaining component code removed for brevity
}

Unit testing

The useApi hook can be easily mocked in tests. The following code uses Jest's spyOn but the same principle should work for other mocking libraries. (I'm also using enzyme in these code samples but that's definitely not required, react-testing-library is also great!)

You may want to test or use the return value of the useApi hook in an assertion.

import { mount } from 'enzyme'
import * as useApiModule from 'react-use-fetch-api'
// ... other imports as needed

let mockReturnValue = {
  id: 1
}

jest.spyOn(useApiModule, 'useApi').mockImplementation(() => ({
  get: () => Promise.resolve(mockReturnValue)
}))

describe('InterestingComponent', () => {
  beforeEach(async () => {
    await act(async () => {
      subject = mount(<InterestingComponent />)
    })
    subject.update()
  })

  it('renders as expected', () => {
    expect(subject.find(ChildComponent)).toHaveLength(1)
    expect(subject.find(ChildComponent).prop('id)).toEqual(mockReturnValue.id)
  })
})

If you are making a request after the user interacts with the component (button click, form submission, etc), you may want to check that the handler had been called with the expected parameters.

import { mount } from "enzyme";
import * as useApiModule from "react-use-fetch-api";
// ... other imports as needed

// important to store the mocked request function in a
// variable so you have access to it in your assertions
let postSpy = jest.fn(() => Promise.resolve({}));
jest.spyOn(useApiModule, "useApi").mockImplementation(() => ({
  post: postSpy,
}));

describe("InterestingComponent", () => {
  describe("form submission", () => {
    beforeEach(() => {
      subject = mount(<AddNewPoetryEntry />);

      subject
        .find("form")
        .first()
        .simulate("submit", { preventDefault: jest.fn() });
    });

    it("calls useApi().post with the expected arguments", () => {
      expect(postSpy).toHaveBeenCalledWith(
        expect.stringContaining("expected string"),
        expect.objectContaining({
          // your expected object here
        })
      );
    });
  });
});

Technologies used