React hook for making API Requests
npm install --save use-request-hook
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>
)
}
Start lib build
yarn
yarn start
Start example
cd example
yarn
yarn start
git clone [email protected]:Selleo/react-developers-workshops-server.git
cd react-developers-workshops-server
yarn
yarn start
MIT © tb