Skip to content

Instantly share code, notes, and snippets.

@Karnak19
Created October 29, 2020 15:53
Show Gist options
  • Select an option

  • Save Karnak19/26d249683ed14604fdaf753a1c23342f to your computer and use it in GitHub Desktop.

Select an option

Save Karnak19/26d249683ed14604fdaf753a1c23342f to your computer and use it in GitHub Desktop.
import { Col, Container, Row } from 'reactstrap';
import './App.css';
import User from './User';
function App() {
return (
<Container>
<Row>
<Col
sm={{ size: 6, offset: 3 }}
xs={{ size: 12, offset: 0 }}
className="mb-4"
>
<User />
</Col>
<Col sm={6}>
<User />
</Col>
<Col sm={6}>
<User />
</Col>
</Row>
</Container>
);
}
export default App;
import { useEffect, useState } from 'react';
function useGet(apiUrl) {
const [datas, setDatas] = useState({});
const [loading, setLoading] = useState(true);
useEffect(() => {
setTimeout(() => {
fetch(apiUrl)
.then((res) => res.json())
.then((res) => {
setDatas(res.results);
setLoading(false);
});
}, 2000);
}, []);
return {
datas,
loading,
};
}
export default useGet;
import { useEffect } from 'react';
import {
Card,
CardImg,
CardText,
CardBody,
CardTitle,
CardSubtitle,
Button,
} from 'reactstrap';
import useGet from './useGet';
function User() {
const { datas, loading } = useGet('https://api.randomuser.me');
useEffect(() => {
console.log(datas[0]);
}, [datas]);
if (loading) {
return <p>Loading...</p>;
}
return (
<Card>
<CardImg
top
width="100%"
src={datas[0].picture.large}
alt="Card image cap"
/>
<CardBody>
<CardTitle>
{datas[0].name.title} {datas[0].name.first} {datas[0].name.last}
</CardTitle>
<CardSubtitle>{datas[0].email}</CardSubtitle>
<CardText>
Some quick example text to build on the card title and make up the
bulk of the card's content.
</CardText>
<Button>Button</Button>
</CardBody>
</Card>
);
}
export default User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment