New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

use-http

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-http

- Code Sandbox Example

  • 0.1.50
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
decreased by-11.23%
Maintainers
1
Weekly downloads
 
Created
Source

useFetch

🐶 React hook for making isomorphic http requests

Need to fetch some data? Try this one out. It's an isomorphic fetch hook. That means it works with SSR (server side rendering).

Examples

Installation

yarn add use-http

Usage

Basic Usage
import useFetch from 'use-http'

function App() {
  const options = { // accepts all `fetch` options
    onMount: true // will fire on componentDidMount
  }
  
  var [data, loading, error, request] = useFetch('https://example.com', options)
  
  // want to use object destructuring? You can do that too
  var { data, loading, error, request } = useFetch('https://example.com')
  
  const postData = () => {
    request.post({
      no: 'way',
    })
  }

  if (error) return 'Error!'
  if (loading) return 'Loading!'
  
  return (
    <>
      <button onClick={postData}>Post Some Data</button>
      <code>
        <pre>{data}</pre>
      </code>
    </>
  )
}
Destructured methods
var [data, loading, error, { post }] = useFetch('https://example.com')

var { data, loading, error, post } = useFetch('https://example.com')

post({
  no: 'way',
})
Relative routes
const [data, loading, error, 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',
})
Coming Soon: abort
const { data, loading, request } = useFetch({
  baseUrl: `https://api.github.com/search`
})

const searchGithub = e => request.get(`/repositories?q=${e.target.value || "''"}`)

<>
  <input onChange={searchGithub} />
  <button onClick={request.abort}>Abort</button>
  {loading ? 'Loading...' : <code><pre>{data}</pre></code>}
</>

Hooks

OptionDescription
useFetchThe base hook
useGetDefaults to a GET request
usePostDefaults to a POST request
usePutDefaults to a PUT request
usePatchDefaults to a PATCH request
useDeleteDefaults to a DELETE request

Options

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

OptionDescriptionDefault
onMountOnce the component mounts, the http request will run immediatelyfalse
baseUrlAllows 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,
  del,
  // delete
} = useFetch({
  url: 'https://example.com',
  baseUrl: 'https://example.com',
  onMount: true
})

Credits

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

Todos

  • Make abortable (add abort to abort the http request)
  • Make work with React Suspense
  • Allow option to fetch on server instead of just having loading state
  • Allow option for callback for response.json() vs response.text()
  • add timeout
  • error handling if no url is passed
  • tests

Keywords

FAQs

Package last updated on 24 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc