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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.1
    2
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.1
    2
  • 1.0.0
    0

Package Sidebar

Install

npm i use-request-hook

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

11.3 kB

Total Files

12

Last publish

Collaborators

  • tomaszbak