Skip to content

Instantly share code, notes, and snippets.

@samipshah100
Last active September 25, 2020 19:41
Show Gist options
  • Save samipshah100/d9f340e276253350d0cdbb8ab3f6e47d to your computer and use it in GitHub Desktop.
Save samipshah100/d9f340e276253350d0cdbb8ab3f6e47d to your computer and use it in GitHub Desktop.
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