This repository was archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
This repository was archived by the owner on Aug 3, 2023. It is now read-only.
useFetch hook #199
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Hello, thanks for this package I've been using for several projects now and it helps a lot!
I didn't find any plans for a possible hook version of the Fetch component, so I transposed it into a useFetch hook and it felt that its place should be here.
Would you be willing take a PR for this?
The first two examples of the README would look like this (of course the name of the hook and the structure of its return value can be changed):
import React from 'react';
import { useFetch } from 'react-request';
function App() {
const [post, { fetching, failed }] = useFetch({
url: 'https://jsonplaceholder.typicode.com/posts/1'
});
if (fetching) {
return <div>Loading data...</div>;
}
if (failed) {
return <div>The request did not succeed.</div>;
}
if (post) {
return (
<div>
<div>Post ID: {post.id}</div>
<div>Post Title: {post.title}</div>
</div>
);
}
return null;
}Multiple requests:
import React from 'react';
import { useFetch } from 'react-request';
function App() {
const [post, readPost] = useFetch({
url: 'https://jsonplaceholder.typicode.com/posts/1'
});
const [, deletePost] = useFetch({
url: 'https://jsonplaceholder.typicode.com/posts/1',
method: 'DELETE'
});
return (
<div>
{readPost.fetching && 'Loading post 1'}
{!readPost.fetching && 'Post 1 is not being fetched'}
<button onClick={() => deletePost.doFetch()}>
Delete Post 1
</button>
</div>
);
}Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request