use-request-hook

1.0.1 • Public • Published

use-request-hook

React hook for making API Requests

NPM JavaScript Style Guide

Install

npm install --save use-request-hook

Usage

import React from 'react'
import axios from 'axios'
import {useRequest} from 'use-request-hook'
 
const api = axios.create({
  baseURL: 'http://localhost:3001/'
})
 
const normalizeData = ({data}) => data
const getPosts = (limit = 5) => api.get(`/posts?_limit=${limit}`).then(normalizeData)
 
const FetchPosts = () => {
  const { isLoading, data: posts = [] } = useRequest(getPosts, [])
 
  return (
    <div>
      {isLoading ? (
        <div>Loading...</div>
      ) : (
        <div>
          {(posts).map(post => (
            <p key={post.id}>{post.id} - {post.title}</p>
          ))}
        </div>
      )}
    </div>
  )
}

Development

Start lib build

yarn
yarn start

Start example

cd example
yarn
yarn start

Start server

git clone git@github.com:Selleo/react-developers-workshops-server.git
cd react-developers-workshops-server
yarn
yarn start

License

MIT © tb

Dependencies (0)

    Dev Dependencies (14)

    Package Sidebar

    Install

    npm i use-request-hook

    Weekly Downloads

    5

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    11.3 kB

    Total Files

    12

    Last publish

    Collaborators

    • tomaszbak