How can I create an infinite scroll using React/Typescript?
I want to display a list of 10 post at first then when the users views the 10th one then another phase of 10 should be loaded on the screen.
Kindly help out with a code demo.
This currently what I have achieved which is currently displaying all post
import PostStyles from "../../../components/ui/createPost/_createpost.module.scss"
import CreatePost from "../createPost/createPost"
import Post from "../post/post"
import Modal from "../createPost/Modal"
import { postType } from "../../../util/types"
type mainSectionType = {
posts:postType[]
}
type postType = {
_id: string;
creator: {
id: string,
name: string,
profilePicture: string
},
createdAt: string,
photos: string[],
post_description: string,
time_posted: string,
}
const MainSection = ({posts}:mainSectionType) => {
return (
<>
<Modal />
<div className={PostStyles.post__create_postContainer}>
<CreatePost />
</div>
<div className={PostStyles.post__posts_container}>
{
posts.map((post, index) => {
return <Post
key={index}
post_desc={post.post_description}
postID={post._id}
username={post.creator.name}
timeposted={post.time_posted}
userImg={post.creator.profilePicture}
photos={post.photos}
/>
})
}
</div>
</>
)
}
export default MainSection
2
Answers
You can use this package for infinite scroll ->
package name: ‘react-infinite-scroll-component’
example:
State :
API response:
HandleNextData function :
Component:
You can use this code for it. I hope it helps.