【发布时间】:2020-05-01 13:59:55
【问题描述】:
我正在尝试连接我的页面以通过 graphql 在滚动时使用无限滚动填充更多项目。现在我遇到了每次滚动时页面刷新的问题,然后在达到 hasMore 最大值时出错。如果有人对如何使这项工作有任何想法,那将是一个巨大的帮助。我正在使用 nextjs、typescript、graphql 和 reactjs 无限滚动
import React, { useState } from 'react';
import gql from 'graphql-tag';
import InfiniteScroll from 'react-infinite-scroller';
import { useQuery } from '@apollo/react-hooks';
import { useRouter } from 'next/router';
import { withApollo } from '../../lib/apollo';
import { ErrorIndicator, LoadingIndicator, SubHeading } from '../../components/Atoms/';
import { Card, SubNav } from '../../components/Molecules/';
import { ArticleList, SixCards } from '../../components/Organisms/';
import { BREAKPOINT } from '../../constants/';
import { countryData } from '../../data/countryData';
export const queryCreator = (categories, count): any => gql`
{
articles(lang: "en", categories: ["${categories}"], count: ${count}){
id
slug
title
content
featuredImage
creator {
name
id
}
categories {
name
id
}
}
}
`;
export const News: React.FC = () => {
const [articleListState, updateArticleList] = useState({
count: 8,
articleList: []
});
const { count, articleList } = articleListState;
const router = useRouter();
const { categories } = router.query;
const categoriesToQuery = categories ? categories.toString() : '';
const formattedQuery = queryCreator(categoriesToQuery, count);
const { loading, error, data } = useQuery(formattedQuery);
if (error) {
return <ErrorIndicator />;
}
if (loading) {
return <LoadingIndicator />;
}
const { articles } = data;
// updateArticleList({ count: 8, articleList: articles });
const hasMore = () => count <= 13;
const loadMore = () => {
if (hasMore) {
const addOne = count + 1;
updateArticleList({ count: addOne, articleList: articles });
}
};
const items = [];
articleList.map((article) =>
items.push(
article && (
<div className="article" key={article.id}>
<Card key={article.id} article={article} size="short" />
<style jsx>{`
.article {
padding: 6px 0;
margin: 0 auto;
}
`}</style>
</div>
)
)
);
console.log(items, count);
return (
<main className="articles-container">
<SubNav />
<SixCards articles={articles} />
<div className="article-list">
<InfiniteScroll
pageStart={0}
loadMore={loadMore}
hasMore={hasMore()}
loader={
<div key={0}>
<SubHeading title="Loading . . ." Tag="h5" />
</div>
}
>
{items}
{/* <ArticleList articles={articleList} /> */}
</InfiniteScroll>
</div>
<style jsx>{`
main.articles-container {
min-height: 80vh;
display: flex;
flex-direction: column;
width: 80%;
margin: 0 auto;
}
.article-list {
display: flex;
flex-direction: column;
justify-content: center;
// width: 100%;
max-width: 1020px;
margin: 10px auto;
align-self: center;
}
@media only screen and (max-width: ${BREAKPOINT}px) {
main.articles-container {
width: 90%;
}
}
`}</style>
</main>
);
};
export default withApollo(News);
【问题讨论】:
-
@IvanKaloyanov 不要进行不会对问题添加任何内容的编辑
-
一个最小可重现的案例会有很大帮助。
-
它的要点是:如何使用InfiniteScroll正确查询GraphQL。此实例需要从查询中获取的“文章”数组,然后使用状态根据“计数”更新列表。这个计数再次注入到查询中,然后用更多要呈现的项目更新文章数组
-
我想知道自从我使用 NextJS 并且那是服务器端渲染之后,它是否一直在刷新
-
在这里找到指导,测试,如果成功将发布:sysgears.com/articles/…
标签: reactjs typescript graphql next.js infinite-scroll