# react-use-request

> Experimental React hook for making HTTP requests with axios

Latest version **0.0.4** (published 2019-03-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-use-request
pnpm add react-use-request
yarn add react-use-request
bun add react-use-request
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2019-03-07 |
| First published | 2019-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 175 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Guilherme Santiago |
| Maintainers | gsantiago |
| Keywords | react, use, request, axios, hook, fetch, network |

## Links

- npm: https://www.npmjs.com/package/react-use-request
- Repository: https://github.com/gsantiago/react-use-request
- Issues: https://github.com/gsantiago/react-use-request/issues
- npm.io page: https://npm.io/package/react-use-request

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.4 (latest) — 2019-03-07
- 0.0.3 — 2019-03-07
- 0.0.2 — 2019-03-07
- 0.0.1 — 2019-02-08

## README

# react-use-request

**Experimental** React hook for making HTTP Requests with axios.

```js
import React from 'react'
import useRequest from 'react-use-request'

function App () {
  const { loading, data, error } = useRequest({
    method: 'get',
    url: '/api/posts'
  })

  if (loading) return <Spinner />

  if (error) return <Error error={error} />

  return (
    <ul>
      {data.map(post => (
        <li key={post.id}>
          {post.title}
        </li>
      ))}
    </ul>
  )
} 
```

## features

- Support the same options as axios does
- Request cancellation out-of-box (no request duplication, no worry about unmounts)

## installation

You need to install axios too:

`yarn add react-use-request axios`

`npm install react-use-request axios --save`

## usage

It supports all options as axios does. But it supports three additional options:

### `lazy: Boolean`

```js
const { request } = useRequest({
  method: 'POST',
  url: '...',
  lazy: true
})

request()
```

### `onSuccess: function (response) `

### `onErrror: function (err) `

## factory

You may want to use a custom instance of axios. To do so, just use the factory function:

```js
import axios from 'axios'
import { createUseRequest } from 'react-use-request'

const customAxios = axios.create({
  baseURL: 'https://example.com/api',
  headers: { Authorization: `Bearer ${token}` }
})

const useRequest = createUserRequest({
  axios: customAxios
})
```

Then, just use `useRequest` as a normal hook.

## license

MIT

---
_Source: https://npm.io/package/react-use-request · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
