Created
August 27, 2021 15:49
-
-
Save rahulbanerjee26/df0c1ca9dfbaf4bb195798965a5abb0e 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
function LoadBackground() { | |
const [isLoading, setIsLoading] = React.useState(true); | |
const [data, setData] = React.useState([]); | |
React.useEffect(() => { | |
const url = "https://randomuser.me/api/?results=15"; | |
fetch(url) | |
.then((response) => response.json()) | |
.then((json) => setData(json['results'])) | |
.catch((error) => console.log(error)); | |
}, []); | |
React.useEffect(() => { | |
if (data.length !== 0) { | |
setIsLoading(false); | |
} | |
console.log(data); | |
}, [data]); | |
return ( | |
<div> | |
{isLoading ? ( | |
<h1>Loading...</h1> | |
) : ( | |
data.map((user) => ( | |
<h1> | |
{user.name.first} {user.name.last} | |
</h1> | |
)) | |
)} | |
</div> | |
); | |
} | |
export default LoadBackground; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment