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: ServidioEC <52542729+ServidioEC@users.noreply.github.com>
- Loading branch information
1 parent
7cb2fba
commit c56435a
Showing
6 changed files
with
39 additions
and
38 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
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 was deleted.
Oops, something went wrong.
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; |
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