Last active
September 25, 2020 19:41
-
-
Save samipshah100/d9f340e276253350d0cdbb8ab3f6e47d to your computer and use it in GitHub Desktop.
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
import React, { Fragment, useState, useEffect } from 'react' | |
import axios from 'axios' | |
function DemoComponent() { | |
const [data, setData] = useState() | |
const url = 'http://dummy.restapiexample.com/api/v1/employees' // sample api | |
const [isLoading, setIsLoading] = useState(false) | |
const [isError, setIsError] = useState(false) | |
useEffect(() => { | |
const fetchData = async () => { | |
setIsError(false) | |
setIsLoading(true) | |
try { | |
const result = await axios(url) | |
setData(result.data) | |
} catch (error) { | |
setIsError(true) | |
} | |
setIsLoading(false) | |
} | |
fetchData() | |
}, []) | |
return (...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment