Skip to content

Commit bca8eaf

Browse files
committed
Add error handling section to README.md
1 parent 33c2f90 commit bca8eaf

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ const { isLoading, data } = useFetch("https://swapi.co/api/people/1", {
5454

5555
```
5656

57+
### Error handling
58+
59+
The `useFetch` hook returns an `error` field at any fetch exception.
60+
The `error` field extends [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
61+
and has `status` and `statusText` fields equal to [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response).
62+
63+
```javascript
64+
...
65+
66+
const Component = () => {
67+
const { isLoading, data, error } = useFetch("https://swapi.co/api/people/1");
68+
69+
if (error) {
70+
return <div>
71+
<p>Code: ${error.status}</p>
72+
<p>Message: ${error.statusText}</p>
73+
</div>
74+
}
75+
76+
...
77+
};
78+
79+
```
80+
5781
### Multiple requests
5882
Multiple `useFetch` in the same file/component supported:
5983

0 commit comments

Comments
 (0)