Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting duplicate elements in react component after fetchMore

I want to fetchMore data when onscroll reaches the bottom. The issue I have is that I get duplicate elements thus console yelling at me as well that I nee unique element keys. I'm getting the correct data in the graphql playground so it's an issue in my react component. Does this logic make sense? What's going wrong here?

class App extends Component {
  isBottom = (fetchMore, data) => {
    window.onscroll = () => {
      if (
        data &&
        data.infiniteScrollMovies &&
        data.infiniteScrollMovies.hasMore &&
        window.innerHeight + document.documentElement.scrollTop === document.documentElement.offsetHeight
      ) {
        console.log('YES!')
        this.fetchMoreData(fetchMore, data)
      }
    }
  }

  fetchMoreData = (fetchMore, data) => {
    fetchMore({
      variables: {
        offset: data.infiniteScrollMovies.newOffset,
      },
      updateQuery: (prev, { fetchMoreResult }) => {
        if (!fetchMoreResult) return prev

        return {
          ...fetchMoreResult,
          infiniteScrollMovies: {
            ...fetchMoreResult.infiniteScrollMovies,
            movies: [...prev.infiniteScrollMovies.movies, ...fetchMoreResult.infiniteScrollMovies.movies],
          },
        }
      },
    })
  }

  render() {
    return (
      <div className="flex flex-column">
        <Query query={ALL_MOVIES} notifyOnNetworkStatusChange={true} fetchPolicy="network-only">
          {({ data, loading, error, fetchMore }) => {
            if (error) return <h1>{error.message}</h1>
            this.isBottom(fetchMore, data)
            return (
              <div>
                {data &&
                  data.infiniteScrollMovies &&
                  data.infiniteScrollMovies.movies.map(m => (
                    <article key={m.id}>
                    // ... rendering an element
                    </article>
                  ))}
           <div>{loading && <h1 className="red text-center">Loading...</h1>}</div>

Here's my query:

export const ALL_MOVIES = gql`
  query infiniteScrollMovies($offset: Int) {
    infiniteScrollMovies(offset: $offset, limit: 16) {
      movies {
        image_url
        title
        id
      }
      hasMore
      newOffset
    }
  }
`
like image 994
karolis2017 Avatar asked Aug 05 '26 19:08

karolis2017


2 Answers

Use the variables parameter in updataeQuery to handle this scenario, basically you need to prevent the appending in cache if already its done.

Change your updateQuery to below

        updateQuery: (prev, { variables, fetchMoreResult }) => {
// Return the prev values, if it allready has the length specified by offset
// thereby preventing duplicate values in cache.
                        if (!fetchMoreResult || 
                           prev.infiniteScrollMovies.movies.length > variables.offset) 
                         return prev
like image 76
MeanMan Avatar answered Aug 08 '26 10:08

MeanMan


try disable the notifyOnNetworkStatusChange option

like image 23
Yasser Nascimento Avatar answered Aug 08 '26 09:08

Yasser Nascimento



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!