Created
December 7, 2021 16:20
-
-
Save yaralahruthik/537aba6aff4656946d4999921a58d88d 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 { useEffect, useState } from 'react'; | |
const ReactWay = () => { | |
const [apiData, setApiData] = useState([]); | |
const [isLoading, setIsLoading] = useState(false); | |
const [error, setError] = useState(''); | |
useEffect(() => { | |
const fetchData = async () => { | |
setIsLoading(true); | |
const response = await fetch( | |
'https://jsonplaceholder.typicode.com/todos' | |
); | |
const data = await response.json(); | |
setApiData(data); | |
setIsLoading(false); | |
}; | |
fetchData().catch((e) => setError(e.message)); | |
}, []); | |
return ( | |
<div> | |
<h1>React Way</h1> | |
{error ? ( | |
<>Oh no, there was an error</> | |
) : isLoading ? ( | |
<>Loading...</> | |
) : apiData ? ( | |
<> | |
<ul> | |
{apiData.map((item) => ( | |
<li key={item.id}>{item.title}</li> | |
))} | |
</ul> | |
</> | |
) : null} | |
</div> | |
); | |
}; | |
export default ReactWay; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment