-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
23 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"usehooks-ts": patch | ||
--- | ||
|
||
Depreciated `useFetch`, see documentation for more information |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
Here is a React Hook which aims to retrieve data on an API using the native Fetch API. | ||
`useFetch` is now **deprecated** and will be removed in the next major release. | ||
|
||
I used a reducer to separate state logic and simplify testing via functional style. | ||
The current implementation is not enterprise-grade and it's a bad example of `useEffect` usage, see this [talk on Youtube](https://www.youtube.com/watch?v=bGzanfKVFeU) for more information. | ||
|
||
The received data is saved (cached) in the application via useRef, but you can use LocalStorage (see [`useLocalStorage()`](/react-hook/use-local-storage)) or a caching solution to persist the data. | ||
Instead, consider these options: | ||
|
||
The fetch is executed when the component is mounted and if the url changes. If ever the url is undefined, or if the component is unmounted before the data is recovered, the fetch will not be called. | ||
|
||
This hook also takes the request config as a second parameter in order to be able to pass the authorization token in the header of the request, for example. Be careful though, the latter does not trigger a re-rendering in case of modification, go through the url params to dynamically change the request. | ||
|
||
**Side notes:** | ||
|
||
- To better understand how this hook works step by step, you can read [this article](https://www.smashingmagazine.com/2020/07/custom-react-hook-fetch-cache-data/) from "Smashing Magazine" which explains how to build a custom react hook to fetch and cache data | ||
- For usage in SSR, consider using [window.fetch.polyfill](https://www.npmjs.com/package/whatwg-fetch) | ||
- It's a very simple fetch hook for basic use cases and learning purposes. | ||
For advanced usages and optimisations, see these other hooks more powerfull like [useSWR](https://swr.vercel.app/), [useQuery](https://github.com/tannerlinsley/react-query) or if you're using Redux Toolkit, consider [RTK Query](https://redux-toolkit.js.org/rtk-query/overview). | ||
- Use your own framework feature (like `RSC` or `getServerSideProps` for NextJS by example) | ||
- Use caching libraries like [`useSWR`](https://swr.vercel.app/) or [`react-query`](https://github.com/tannerlinsley/react-query) | ||
- Use the React built-in `use` hook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters