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

yet-another-usefetch

v0.2.2

Published

This implementation of a useFetch hook adds the ability to conditionally call REST endpoints which is missing from other useFetch functions I've seen.

Downloads

3

Readme

useFetch

This implementation of a useFetch hook adds the ability to conditionally call REST endpoints which is missing from other useFetch functions I've seen.

Features

  • Noop State
  • Loading state
  • Error state including handling of HTTP error status
  • Accepts header token
  • Conditional fetch
  • Refetch

Useage

  yarn add yet-another-usefetch
import { useFetch } from "yet-another-usefetch";

ARGS

url string default: null

If the url exists as an argument of the hook, the request will be made immediately

method string default: GET

Set the method of HTTP request you are making

body object default: null

The data payload of the network request. Note: The body does not need to be stringified beforehand as it is stringified inside the hook function.

headers object default: null

Any HTTP headers that you'd like to add to the request. Note: There are default headers already included in the hook listed below. Additionally, if you want to use an auth token, read below

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

addHeaders boolean default: true

If you'd like to send a request without headers, you can pass this argument

token string default: null

Useful if you'd like to add your own authorization token. It uses the following format where token is the value of your argument

{
  Authorization: `Bearer ${token}`
}

API

noop bool

Return true if the fetch url has not been called

loading bool

Returns true if the fetch url has been set but there has not been a response

response object

Returns when the request resolves successfully

error object

Returns if there was a problem executing the request

setFetchUrl function

Initiates the fetch request if a url wasn't supplied beforehand. Once the url is set the request initiates. Accepts a string url.

setFetchBody function

When updating server data this function sets the body. Useful if you are sending user inputted data. Used in conjuction with setFetchUrl()

rerunFetch function

Initiates another request to the server. Accepts a boolean argument, typically true, which gets reset to false after the request resolves or errors.

Examples

Code Sandbox Example

Simple GET

  const {loading, response, error} = useFetch('https://example.com/users')

Conditional GET

  const {loading, response, error, setFetchUrl, rerunFetch} = useFetch()
  const getData = () => setFetchUrl('https://example.com/users')
  const getNewData = () => rerunFetch(true)

Conditional POST

  const {noop, loading, response, error, setFetchUrl, setFetchBody} = useFetch(null, 'POST')
  const handleSubmit = (formData) => {
    setFetchBody(formData);
    setFetchUrl('https://example.com/users')
  }

Requires a peer dependency of react: >=16.8.0