Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react hooks. hasMore does not work well. #273

Open
johnvonneumann7 opened this issue Feb 14, 2021 · 2 comments
Open

react hooks. hasMore does not work well. #273

johnvonneumann7 opened this issue Feb 14, 2021 · 2 comments

Comments

@johnvonneumann7
Copy link

api server: django
If res.data.next is null, I want to disable the next reload, but the following code will disable it after one round, and if the page has up to 3, it will get up to 4, and I will get a 404 error.

import React, { useState } from 'react';
import InfiniteScroll from 'react-infinite-scroller';
import axios from 'axios';
import Box from '@material-ui/core/Box';
import Grid from '@material-ui/core/Grid';
import CircularProgress from '@material-ui/core/CircularProgress';
import { Video } from '../components/Types';
import VideoCard from '../components/VideoCard';

type Props = {
  to: string;
  tag?: string;
};

export default function VideoList(props: Props) {
  const [data, setData] = useState<Video[]>([]);
  const [hasMore, setHasMore] = useState(true);

  const loadMore = (page: number) => {
    axios
      .get(`/api/videos/${props.to}`, {
        params: { page: page, tag: props.tag },
      })
      .then((res) => {
        setData([...data, ...res.data.results]);
        if (!res.data.next) setHasMore(false);
      });
  };

  return (
    <InfiniteScroll
      pageStart={0}
      loadMore={loadMore}
      hasMore={hasMore}
      loader={
        <Box key={0} textAlign="center">
          <CircularProgress />
        </Box>
      }
    >
      <Grid container spacing={2}>
        {data.map((video) => (
          <Grid key={video.pk} item xs={12} sm={6} md={4} lg={3} xl={2}>
            <VideoCard video={video} />
          </Grid>
        ))}
      </Grid>
    </InfiniteScroll>
  );
}

image
image

What should I do in this case?

@apenab
Copy link

apenab commented Mar 30, 2021

I would recommend using react-query with the function Inifinity Query. I use that package with this package and it's work properly.

@jack-annexdistribution
Copy link

You may just have to call setHasMore before you call setData

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants