Add infinite scroll to post list

This commit is contained in:
riggraz
2019-09-04 21:12:07 +02:00
parent 2a42d3069c
commit f9f2b291d6
8 changed files with 98 additions and 27 deletions

View File

@@ -1,5 +1,7 @@
import * as React from 'react';
import InfiniteScroll from 'react-infinite-scroller';
import PostListItem from './PostListItem';
import Spinner from '../shared/Spinner';
@@ -9,14 +11,24 @@ interface Props {
posts: Array<IPost>;
areLoading: boolean;
error: string;
handleLoadMore(): void;
page: number;
hasMore: boolean;
}
const PostList = ({ posts, areLoading, error }: Props) => (
const PostList = ({ posts, areLoading, error, handleLoadMore, page, hasMore }: Props) => (
<div className="box postList">
{ areLoading ? <Spinner /> : null }
{ error ? <span className="error">{error}</span> : null }
{
posts.map((post, i) => (
<InfiniteScroll
initialLoad={false}
loadMore={handleLoadMore}
threshold={50}
hasMore={hasMore}
loader={<Spinner />}
useWindow={true}
>
{posts.map((post, i) => (
<PostListItem
title={post.title}
description={post.description}
@@ -24,8 +36,8 @@ const PostList = ({ posts, areLoading, error }: Props) => (
key={i}
/>
))
}
))}
</InfiniteScroll>
</div>
);