generated from UnBArqDsw2021-1/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Emily Sousa <diassousa@yahoo.com.br>
- Loading branch information
1 parent
ecc3e01
commit a315202
Showing
1 changed file
with
32 additions
and
0 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,32 @@ | ||
import React from "react"; | ||
import { useState, useEffect, useCallback } from "react"; | ||
|
||
type Hook = (url: string, setMenuItem: (menuItem: string) => void) => boolean; | ||
|
||
interface IuseFetch { | ||
loading: boolean; | ||
} | ||
|
||
const useFetch: Hook = (url, setMenuItem) => { | ||
const [loading, setLoading] = useState(true); | ||
|
||
const getMenuItems = useCallback(async () => { | ||
try { | ||
const response = await fetch(url); | ||
const data = await response.json(); | ||
setLoading(false); | ||
setMenuItem(data); | ||
} catch (error) { | ||
setLoading(false); | ||
throw new Error("useFetch: Can't access url"); | ||
} | ||
}, [url, setMenuItem]); | ||
|
||
useEffect(() => { | ||
getMenuItems(); | ||
}, [getMenuItems]); | ||
|
||
return loading; | ||
}; | ||
|
||
export default useFetch; |