Created
March 23, 2019 11:48
-
-
Save hardikmodi1/b16c9e42b6232329ebf69586113e5857 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { useState, useEffect } from 'react'; | |
| export const useApi = (url, dependencies) => { | |
| const [isLoading, setIsLoading] = useState(false); | |
| const [data, setData] = useState(null); | |
| useEffect(() => { | |
| setIsLoading(true); | |
| console.log('Sending Http request to URL: ' + url); | |
| fetch(url) | |
| .then(response => { | |
| if (!response.ok) { | |
| console.log("Error in fetching data"); | |
| } | |
| return response.json(); | |
| }) | |
| .then(data => { | |
| setIsLoading(false); | |
| setData(data); | |
| }) | |
| .catch(err => { | |
| console.log(err); | |
| setIsLoading(false); | |
| }); | |
| }, dependencies); | |
| return [isLoading, data]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment