react-request-hook-client
A helper react component to make HTTP requests, powered by React Hooks, insipred by Apollo GraphQL Query
component. It supports both declarative and hooks ways.
Installing
npm
$ npm install --save react-request-hook-client
or Yarn
$ yarn add react-request-hook-client
Requirements
React >= 16.8.0
Get Started
See live example on CodeSandBox:
use <Request /> component
GET
import Request useRequest from "react-request-hook-client"; { return <Request ="https://jsonplaceholder.typicode.com/todos/1"> data return <div>Get using declarative way: JSON</div>; </Request> ;}
POST and others
{ return <> <div>declarative post: </div> <Request ="POST" ="https://jsonplaceholder.typicode.com/posts" = > doRequest loading error data return <> <div>loading: ``</div> <div>data: JSON</div> <div>error: JSON</div> <button = > Send request </button> </> ; </Request> </> ;}
Use Hooks
GET
{ const error loading data = ; if loading return <div>loading...</div>; if error return null; return <div>hooks way: JSON</div>;}
POST and others
{ const doRequest error loading data = ; return <> <div>Post Hook</div> <div>loading: ``</div> <div>data: JSON</div> <div>error: JSON</div> <button = > Send request </button> </> ;}
API
Request component
Props
Props | Type | Default Value | Description |
---|---|---|---|
url | String | The HTTP request url | |
method | String | GET | The HTTP request method, supports GET, POST, PUT, DELETE |
variables | Object | Variables for the request. If it is a GET request, variables will be converted to query strings. If it is a POST/PUT/DELETE request, variables will be the request body. | |
headers | Object | {"Content-Type": "application/json"} | Additional headers |
fire | Boolean | TRUE | Whether to fire the request immediately, used to finely control the time when the request will fire. |
onSuccess | (data) => Void | Called on request successfully returned | |
onError | (error) => Void | Called on error ocurred during the request | |
onComplete | () => Void | Called on request finished, whether it succeeded or failed |
The children
passed to the <Request/>
component must be a function, it has the following signature when the request method is GET:
{ return // a react component }
POST and others:
{ return // a react component }
Arguments explanation:
- loading - Whether the request is pending.
- error - Error details when request encounters HTTP errors.
- data - Data returned by the server.
The function MUST return a single React component according to JSX rules, or null if no component will be returned.
useRequest hook
The useRequest
hook takes the same props/arguments as the <Request/>
component does, and returns the following object upon using:
const loading error data doRequest =
In addition to specify variables
at the declaration time, the doRequest
function also takes an additional variables object as its argument:
<button => Send request</button>
It also has return values, which are:
{ success: true, response }
- when the HTTP request succeeds.{ success: false, error }
- when the HTTP request fails.
These values can help users to deal with response data at the time when the event triggers.
Issues
If you have any questions or find a bug of this library, please feel free to open an issue.
License
MIT