-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
61 lines (55 loc) · 1.3 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import "./App.css";
import { useState, useEffect } from "react";
import Card from "./Card";
import { FaGreaterThan, FaLessThan } from 'react-icons/fa';
export default function App() {
const [index, setIndex] = useState(0);
const [user, setUser] = useState([{ id: "", name: "", email: "", username: "" }]);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/users')
.then(res => res.json())
.then(res => setUser(res));
}, []);
const { id, name, username, email } = user[index];
const lessThan = () => {
if (index <= 0) {
setIndex(user.length - 1);
} else {
setIndex(index - 1);
}
};
const greaterThan = () => {
if (index >= user.length - 1) {
setIndex(0);
} else {
setIndex(index + 1);
}
};
return (
<div className = "boader">
<p> USER DETAILS</p>
<Card>
<div>
Name : {name}
</div>
<div>
id : {id}
</div>
<div>
Username : {username}
</div>
<div>
email : {email}
</div>
</Card>
<div className="buttons">
<button onClick={lessThan}>
<FaLessThan />
</button>
<button onClick={greaterThan}>
<FaGreaterThan />
</button>
</div>
</div>
);
}