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

ruse-fetch

v2.2.4

Published

React hook to use Fetch in async mode

Downloads

90

Readme

Build Status

ruse-fetch

A simple React hook to use fetch with Suspense and Redux. Ideal for universal apps.

Getting started

Install it with:

npm install ruse-fetch

To make it work, you need to add our reducer to your Redux store:

import { createStore, combineReducers } from 'redux'
import { fetchReducer } from 'ruse-fetch'

const reducers = {
  /* your other reducers... */
  useFetch: fetchReducer
}
const store = createStore(combineReducers(reducers))

Finally, to use it, just do:

import { useFetch } from 'ruse-fetch'

const MyComponent = ({ id }) => {
  const data = useFetch(`https://jsonplaceholder.typicode.com/users/${id}`)
  return <div>{data.name}</div>
}

The parameters are the same as for fetch, so if you need to set any options, just pass them along:

const data = useFetch(url, {
  method: 'POST',
  headers: { Authorization: myAuth }
})

Handling loading and errors

Instead of creating our own solution for these cases, we use the already available tools in react. Just use <Suspense> for the loading status, and error boundaries for errors:

const MyComponentWrapper = ({ id }) =>
  <ErrorBoundary fallback="Not Found">
    <Suspense fallback="Loading...">
      <MyComponent id={id} />
    </Suspense>
  </ErrorBoundary>

Server-side rendering

This library is compatible with server side rendering but, unfortunately, React doesn't support suspense on the server yet. While it doesn't get released, you can use react-async-ssr to bridge the gap.

If you're already sending your Redux store from server to client, things will just work. The server will wait for all fetch requests before sending its response, and the client won't repeat any fetch during hydration, but will make any new fetch it requires later.

Cache

If multiple components use the same URL, only a single request will be made. This caching is based only on the URL. If you need to override this, you can pass an alternative cache key as the third parameter, and any components sharing the key will make a single request.

Using outside of render

You might need to make fetch calls for reasons other than data loading, such as form submitting, or event tracking. In these cases, useFetch wouldn't work, as you need to make the call in an event handler, and not during render. You can use fetch directly, but if you ever need it to use and share the cache with other useFetch calls, you can use useFetchCb:

const doFetch = useFetchCb(url) // Didn't fetch anything yet
const handleClick = e => {
  // ...
  doFetch() // Fetch is executed here
}

Getting metadata

To keep things simple, useFetch returns the JSON data directly, but sometimes you need to get the status code or headers too. For these cases, we also provide a useFetchMeta hook, which will return an object with the status, header, and timestamp of any previous useFetch call:

const data = useFetch(url)
const { status, headers } = useFetchMeta(url)

You can pass a cache key as the second parameter when needed. Note that it doesn't receive the options, as it will never initiate a fetch, and they are not needed to identify a specific call: only the url and cache key are needed.

Tips & tricks

Sometimes you need to fetch conditionally during render, but hooks can't be called conditionally. No problem: just set the URL to null, and useFetch will be a no-op.