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

use-suspense

v0.1.59

Published

- <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/embed/km04k9k9x5'>Code Sandbox Example</a>

Downloads

2

Readme

Examples

  • Code Sandbox Example

Installation

yarn add use-http    or    npm i -S use-http

Usage

Basic Usage

import useFetch from 'use-http'

function Todos() {
  const options = { // accepts all `fetch` options
    onMount: true // will fire on componentDidMount
  }
  
  const todos = useFetch('https://example.com/todos', options)
  
  const addTodo = () => {
    todos.post({
      title: 'no way',
    })
  }

  if (todos.error) return 'Error!'
  if (todos.loading) return 'Loading...'
  
  return (
    <>
      <button onClick={addTodo}>Add Todo</button>
      {todos.data.map(todo => (
        <div key={todo.id}>{todo.title}</div>
      )}
    </>
  )
}

Destructured methods

var [data, loading, error, request] = useFetch('https://example.com')

// want to use object destructuring? You can do that too
var { data, loading, error, request } = useFetch('https://example.com')

request.post({
  no: 'way',
})

Relative routes

const request = useFetch({
  baseUrl: 'https://example.com'
})

request.post('/todos', {
  no: 'way'
})

Helper hooks

import { useGet, usePost, usePatch, usePut, useDelete } from 'use-http'

const [data, loading, error, patch] = usePatch({
  url: 'https://example.com',
  headers: {
    'Content-type': 'application/json; charset=UTF-8'
  }
})

patch({
  yes: 'way',
})

Abort

const githubRepos = useFetch({
  baseUrl: `https://api.github.com/search/repositories?q=`
})

// the line below is not isomorphic, but for simplicity we're using the browsers `encodeURI`
const searchGithubRepos = e => githubRepos.get(encodeURI(e.target.value))

<>
  <input onChange={searchGithubRepos} />
  <button onClick={githubRepos.abort}>Abort</button>
  {githubRepos.loading ? 'Loading...' : githubRepos.data.items.map(repo => (
    <div key={repo.id}>{repo.name}</div>
  ))}
</>

Hooks

| Option | Description | | --------------------- | ---------------------------------------------------------------------------------------- | | useFetch | The base hook | | useGet | Defaults to a GET request | | usePost | Defaults to a POST request | | usePut | Defaults to a PUT request | | usePatch | Defaults to a PATCH request | | useDelete | Defaults to a DELETE request |

Options

This is exactly what you would pass to the normal js fetch, with a little extra.

| Option | Description | Default | | --------------------- | --------------------------------------------------------------------------|------------- | | onMount | Once the component mounts, the http request will run immediately | false | | baseUrl | Allows you to set a base path so relative paths can be used for each request :) | empty string |

const {
  data,
  loading,
  error,
  request,
  get,
  post,
  patch,
  put,
  delete // don't destructure `delete` though, it's a keyword
  del,   // <- that's why we have this (del). or use `request.delete`
} = useFetch({
  url: 'https://example.com',
  baseUrl: 'https://example.com',
  onMount: true
})

or

const [data, loading, error, request] = useFetch({
  url: 'https://example.com',
  baseUrl: 'https://example.com',
  onMount: true
})

const {
  get,
  post,
  patch,
  put,
  delete // don't destructure `delete` though, it's a keyword
  del,   // <- that's why we have this (del). or use `request.delete`
} = request

Credits

use-http is heavily inspired by the popular http client axios

Feature Requests/Ideas

If you have feature requests, let's talk about them in this issue!

Todos

  • [ ] Make work with React Suspense current example WIP
  • [ ] Allow option to fetch on server instead of just having loading state
  • [ ] Allow option for callback for response.json() vs response.text()
  • [ ] add timeout
  • [ ] add debounce
  • [ ] if 2nd param of post or one of the methods is a string treat it as query params
  • [ ] error handling if no url is passed
  • [ ] tests
  • [ ] port to typescript
  • [ ] badges, I like the way these guys do it