Image by Lisa Vanthournout from Unsplash
DESCRIPTION
Let's create a custom hook for fetching data with the browser fetch which we'll use for getting some Pokemons from pokeAPI.
Preview
Fetch them all!
Summary
- We used a combination of useReducer and useEffect hooks to create our custom
useFetch
hook. - Additionally, browser fetch with async/await and try...catch was used to get the data from an API through an endpoint.
- Then, we created an example application using pokeAPI and Material UI.
- As
fetch
requires a little bit of boilerplate and doesn't catch all the possible errors (need to use.ok
check), you might consider axios instead for theuseFetch
hook (and call it e.g.useApi
😀) - There is a way to completely omit this custom hook by using react-query 🙈. It's an amazing library for data fetching with support for cache, etc.
- Feel free to extend the app as you need - maybe create astonishing Pokedex online? (just throwing an idea)