Skip to content

Commit

Permalink
update useFetch
Browse files Browse the repository at this point in the history
add useCallback
  • Loading branch information
LieuMai committed Aug 29, 2021
1 parent ff56bac commit 2c130de
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { useState, useEffect } from 'react';
import { useState, useEffect,useCallback } from 'react';

export const useFetch = (url) => {
const [loading, setLoading] = useState(true)
const [products, setProducts] = useState([])

const getProducts = async () => {
const getProducts = useCallback(async () => {
const response = await fetch(url)
const products = await response.json()
setProducts(products)
setLoading(false)
}
},[url])

useEffect(() => {
getProducts()
}, [url])
}, [url,getProducts])

return { loading,products }
};

0 comments on commit 2c130de

Please sign in to comment.