use-promise-request
v0.0.6
Published
npm:
Downloads
3
Maintainers
Readme
Installation
npm:
npm install --save use-promise-request
yarn:
yarn add use-promise-request
import { useState } from 'react';
import { useRequest } from 'use-promise-request';
const _fetchUsers = async () => {
const res = await fetch('https://jsonplaceholder.typicode.com/users');
return res.json();
};
export const App = () => {
const { request, loading } = useRequest();
const [users, setUsers] = useState([]);
const getUsers = () => {
request(_fetchUsers()).then(setUsers);
};
return (
<>
<div>
<button onClick={getUsers}>get users</button>
</div>
{loading ? <span>loading...</span> : JSON.stringify(users)}
</>
);
};