Skip to content

Instantly share code, notes, and snippets.

@rahulbanerjee26
Created August 27, 2021 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulbanerjee26/df0c1ca9dfbaf4bb195798965a5abb0e to your computer and use it in GitHub Desktop.
Save rahulbanerjee26/df0c1ca9dfbaf4bb195798965a5abb0e to your computer and use it in GitHub Desktop.
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