Skip to content

Commit

Permalink
#15 Add infinity scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraBergstrom committed Jun 27, 2023
1 parent 6b9671b commit 21f7c8f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/pages/posts/PostsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { useLocation } from "react-router-dom";
import { axiosReq } from "../../api/axiosDefaults";

import NoResults from "../../assets/no-results.png";
import InfiniteScroll from "react-infinite-scroll-component";
import {fetchMoreData} from "../../utils/utils"

function PostsPage({ message, filter = "" }) {
const [posts, setPosts] = useState({ results: [] });
Expand Down Expand Up @@ -60,9 +62,18 @@ function PostsPage({ message, filter = "" }) {
{hasLoaded ? (
<>
{posts.results.length ? (
posts.results.map((post) => (
<Post key={post.id} {...post} setPosts={setPosts} />
))
<InfiniteScroll
children={
posts.results.map((post) => (
<Post key={post.id} {...post} setPosts={setPosts} />
))
}
dataLength={posts.results.length}
loader={<Asset spinner />}
hasMore={!!posts.next}
next={() => fetchMoreData(posts, setPosts)}
/>

) : (
<Container className="appStyles.Content">
<Asset src={NoResults} message={message} />
Expand Down

0 comments on commit 21f7c8f

Please sign in to comment.