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.32
  • 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

import useFetch from 'use-http'

function App() {
  // add whatever other options you would add to `fetch` such as headers
  const options = {
    method: 'POST',
    body: {}, // whatever data you want to send
  }
  
  var [data, loading, error] = useFetch('https://example.com', options)
  
  // want to use object destructuring? You can do that too like:
  var { data, loading, error } = useFetch('https://example.com', options)
  
  if (error) {
    return 'Error!'
  }
  
  if (loading) {
    return 'Loading!'
  }
  
  return (
    <code>
      <pre>{data}</pre>
    </code>
  )
}

Or you can use one of the nice helper hooks. All of them accept the second options parameter.

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

function App() {
  const [data, loading, error] = useGet('https://example.com')
  
  if (error) {
    return 'Error!'
  }
  
  if (loading) {
    return 'Loading!'
  }
  
  return (
    <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

OptionDescription
optionsThis is exactly what you would pass to the normal js fetch

FAQs

Package last updated on 17 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