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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-fetch-hooks

v3.0.0

Published

A set of react hooks to work with the fetch API and gracefully parse & deal with HTTP errors

Downloads

18

Readme

React Fetch Hooks

A set of react hooks to work with the fetch API and gracefully parse & deal with HTTP errors.

const { isFetching, isFetched, error, body } =
		useFetch(`https://api.example.com/`);

or

const { isFetching, isFetched, error, body: result, fetch: saveThing } =
		useLazyFetch(`https://api.example.com/`, {
			method: "POST"
		});

// ...later, maybe in response to a user action
saveThing();

See more in-depth examples.

Install

npm install react-fetch-hooks

This library makes use of and assumes the fetch API is available on the global scope. You will need to polyfill it if it is not available in the current environment.

Polyfill the Browser

All evergreen browsers already support fetch. You should really only need a polyfill if you want this library to work in IE 🤮. See the full support matrix for fetch.

Try using Github's fetch polyfill or add a reference to polyfill.io if you decide you need to polyfill.

Polyfill the Server

If using this library in node, make use of the node-fetch library to polyfill fetch:

global.fetch = require("node-fetch");

Do that at the beginning of the entry point for your app and then you can use react-fetch-hooks as normal.

Examples

import React from "react";
import { useFetch } from "react-fetch-hooks";

const MyBanana = ({ id }) => {
	const { isFetching, isFetched, error, body: banana } =
		useFetch(`https://api.example.com/bananas/${id}`);

	if (isFetching) {
		return <span>Loading...</span>;
	}

	if (error) {
		// the error message will be parsed from the HTTP response, if available
		return <span>Some shit broke: {error}</span>;
	}

	return <span>My banana is {banana.color}!</span>;
};

This will make a GET request to api.example.com whenever the id prop passed to the component changes. It will then return the status of the response body being loaded as well as the body itself when it is ready. The useFetch hook takes all the same parameters/options as the standard fetch API.

Lazy Evaluation

You can also use the hook to lazily create a function that can be invoked later:

import React from "react";
import { useLazyFetch } from "react-fetch-hooks";

const BananaEditor = ({ color }) => {
	const { isFetching: isSaving, error, fetch: saveBanana } =
		useLazyFetch({
			url: "https://api.example.com/bananas/",
			method: "POST",
			body: JSON.stringify({ color })
		});

	if (isSaving) {
		return <span>Saving...</span>;
	}

	if (error) {
		return <span>Some shit broke: {error}</span>;
	}

	return <button onClick={saveBanana}>Save new banana with color: {color}</button>;
};

This will not invoke the fetch until the button is clicked.

Or, if you want the request body to come from the lazy function's parameters:

import React from "react";
import { useLazyFetch } from "react-fetch-hooks";

const BananaEditor = ({ color }) => {
	const { isFetching: isSaving, error, fetch: saveBanana } =
		useLazyFetch({
			url: "https://api.example.com/bananas/",
			method: "POST"
		});

	if (isSaving) {
		return <span>Saving...</span>;
	}

	if (error) {
		return <span>Some shit broke: {error}</span>;
	}

	return <button onClick={() => saveBanana({ color })}>Save new banana with color: {color}</button>;
};

This is equivalent to the example above except the request body is provided to the lazy function rather than at the time of hook creation.

Polling on an Interval

You can pass a refreshInterval parameter to any of the fetch hooks to refresh the data (do the fetch again) after a certain amount of time has passed. This could be useful to poll the server changes on a set interval:

import React from "react";
import { useFetch } from "react-fetch-hooks";

const MyBanana = ({ id }) => {
	const { body: banana } = useFetch({
		url: `https://api.example.com/bananas/${id}`,
		refreshInterval: 10000
	});

	if(!banana) return null;
	return <span>My banana is {banana.color}!</span>;
};

This will make a GET request and update the data from the server every 10 seconds.

Reset Fetched Data

You can pass a resetDelay parameter to any of the fetch hooks to reset the data after a certain amount of time has passed. This could be useful to clear the "saved" flag after a short amount of time:

import React from "react";
import { useLazyFetch } from "react-fetch-hooks";

const BananaEditor = ({ color }) => {
	const { isFetching: isSaving, isFetched: isSaved, fetch: saveBanana } =
		useLazyFetch("https://api.example.com/bananas/", {
			method: "POST",
			body: JSON.stringify({ color }),
			resetDelay: 3000
		});

	if (isSaving) {
		return <span>Saving...</span>;
	}

	if (isSaved) {
		return <span>You just saved a great banana! Congrations to you!</span>;
	}

	return <button onClick={saveBanana}>Save new banana with color: {color}</button>;
};

This will clear the isSaved flags 3 seconds after the POST finishes successfully.

Conditional Fetching

Since you shouldn't wrap your hooks in conditionals, if you want to conditionally fetch, you can pass a null or empty URL to the hook which will tell it not to do anything:

import React from "react";
import { useFetch } from "react-fetch-hooks";

const MyBanana = ({ id }) => {
	const { body: banana } = useFetch(id ? `https://api.example.com/bananas/${id}` : null);

	if (!banana) return null;
	return <span>My banana is {banana.color}!</span>;
};

This will only make the GET request when a valid id has been passed to the component. Any of the following will also accomplish the same result:

// all of these will do nothing
useFetch();
useFetch("");
useFetch({
	url: null,
	headers: {
		"Content-Type": "application/json"
	}
});
useFetch({
	// missing URL
	headers: {
		"Content-Type": "application/json"
	}
});

Bearer Tokens

You can pass specific headers to the hook but setting an Authorization header with a bearerToken is one we use often enough that we built in a shortcut for it:

import React from "react";
import { useFetch } from "react-fetch-hooks";

const MyBanana = ({ id, authToken = "mytoken" }) => {
	const { body: banana } = useFetch(`https://api.example.com/bananas/${id}`, {
		bearerToken: authToken
	});

	if (!banana) return null;
	return <span>My banana is {banana.color}!</span>;
};

This will make a GET request adding an Authorization header with the value Bearer mytoken. bearerToken can also be a Promise, a function, or a function that returns a Promise (an async function) and the hook will wait for the bearerToken to resolve before making the request.

Build/Run Locally

After cloning this repo, run:

npm install
npm run lint
npm test
npm run compile