An Interest In:
Web News this Week
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
- March 25, 2024
- March 24, 2024
React Hooks for Data Part 1 - Fetching Data
So the large hype on React Hooks is over and the community isn't talking about them that much anymore. But seriously hooks are beasts. In this post I'll explain how you can use React Hooks to fetch and submit data to any API (I'll use REST in this guide).
Writing your own hook
We'll start with writing our first hook to fetch books from an API. Here is the example code:
import { useEffect, useState } from 'react'// The hook is just a simple function which we can exportexport const useFetchBooks = () => { // First we define the necessary states for our hook // this includes book, the loading state and potential errors const [books, setBooks] = useState([]) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) // useEffect can be compared to componentDidMount, // componentDidUpdate and componentDidUnmount // read more about useEffect here: // https://reactjs.org/docs/hooks-effect.html useEffect(() => { // First we set the loading and error states setLoading(true) setError(null) fetch('https://library.com/api/books') .then(res => res.json()) .then(json => { setLoading(false) if (json.books) { setBooks(json.books) } else { setBooks([]) } }) .catch(err => { setError(err) setLoading(false) }) }, []) return { books, loading, error }}
Now this looks complicated but really it isn't. Remove the comments and it will be a really short function which fetches data and updates states.
Now that we have the hook, we can use it in a component like this:
import React from 'react'import { useFetchBooks } from './utils/hooks'const BookList = () => { // use your own hook to load the data you need const { books, loading, error } = useFetchBooks() if (loading) return <div>Loading...</div> if (error) return <div>{error}</div> return ( <div> { books && books.length > 0 && books.map(book => <div key={book.id}>{book.title}</div> } </div> )}export default BookList
Use parameters in your hook
Now our hook works fine but it's still a bit stupid. Lets say you want your users to be able to search for books in the list. You could do it like this:
import { useEffect, useState } from 'react'// Note here the new parameter we pass into the hook called "search"// this will be used to search the api for specific booksexport const useFetchBooks = (search) => { const [books, setBooks] = useState([]) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) setError(null) // Change the apiUrl according to the search string const apiUrl = search && search.length > 0 ? `https://library.com/api/books?search=${search}` : 'https://library.com/api/books' fetch(apiUrl) .then(res => res.json()) .then(json => { setLoading(false) if (json.books) { setBooks(json.books) } else { setBooks([]) } }) .catch(err => { setError(err) setLoading(false) }) // This is important. We pass the new search parameter into // the empty array we had before. This means, the effect // will run again if this parameter changes }, [search]) return { books, loading, error }}
Now you can use the hook like this in your component:
const { books, loading, error } = useFetchBooks(props.search)
This should be enough for part 1 and should clarify how to use hooks to fetch data from any API.
I'll update this post with a link to part 2 as soon as I'm done with it.
Have fun!
Original Link: https://dev.to/bdbch/react-hooks-for-data-part-1-fetching-data-28ge
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To