Skip to content

softkraftco/use-request-hook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

React hook for making API Requests

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published