Skip to content

Commit b08454d

Browse files
committed
feat: 🎸 added infinite scroll question
added infinite scroll question
1 parent 1a9b11e commit b08454d

File tree

4 files changed

+242
-1
lines changed

4 files changed

+242
-1
lines changed

2024 Prep/machine_coding/react/package-lock.json

Lines changed: 152 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2024 Prep/machine_coding/react/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
13+
"axios": "^1.6.8",
1314
"react": "^18.2.0",
1415
"react-dom": "^18.2.0"
1516
},

2024 Prep/machine_coding/react/src/App.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#root {
22
max-width: 1280px;
33
margin: 0 auto;
4-
padding: 2rem;
54
text-align: center;
65
}
76

@@ -40,3 +39,7 @@
4039
.read-the-docs {
4140
color: #888;
4241
}
42+
43+
.container {
44+
height: 100vh;
45+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React, { useState, useEffect, useRef } from 'react'
2+
import Loader from './Loader'
3+
import axios from 'axios'
4+
5+
Loader
6+
const URL = 'https://jsonplaceholder.typicode.com/posts'
7+
8+
export default function App() {
9+
const [data, setData] = useState([])
10+
const [currentPage, setCurrentPage] = useState(1)
11+
const observerTarget = useRef(null)
12+
13+
// * Fetch data and update the
14+
const getData = async (pageNumber: number) => {
15+
try {
16+
const res = await axios.get(URL, {
17+
params: {
18+
_page: String(pageNumber),
19+
_limit: '10',
20+
},
21+
})
22+
const result = res.data
23+
setData(prevData => [...new Set([...prevData, ...result])])
24+
setCurrentPage(prevPage => prevPage + 1)
25+
} catch (error) {
26+
console.log(error)
27+
}
28+
}
29+
30+
const handleObserver = (entries: IntersectionObserverEntry[]) => {
31+
if (entries[0].isIntersecting) {
32+
getData(currentPage)
33+
}
34+
}
35+
36+
useEffect(() => {
37+
const options = {
38+
root: null,
39+
rootMargin: '0px',
40+
threshold: 1.0,
41+
}
42+
43+
const observer = new IntersectionObserver(handleObserver, options)
44+
45+
if (observerTarget.current) {
46+
observer.observe(observerTarget.current)
47+
}
48+
49+
return () => {
50+
if (observerTarget.current) {
51+
observer.unobserve(observerTarget.current)
52+
}
53+
}
54+
}, [currentPage])
55+
56+
return (
57+
<div
58+
style={{
59+
display: 'flex',
60+
flexDirection: 'column',
61+
justifyContent: 'center',
62+
alignItems: 'center',
63+
}}
64+
>
65+
{data.map(({ id, title, body }) => (
66+
<div
67+
style={{
68+
padding: '30px 20px',
69+
border: '1px solid red',
70+
maxWidth: '50%',
71+
}}
72+
key={id}
73+
>
74+
<b>
75+
{id} - {title}
76+
</b>
77+
<p>{body}</p>
78+
</div>
79+
))}
80+
<div ref={observerTarget}>
81+
<Loader />
82+
</div>
83+
</div>
84+
)
85+
}

0 commit comments

Comments
 (0)