question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

loadMore function get called infinitely

See original GitHub issue

I use React-Redux to pass loadMore and hasMore

Code:

import * as React from 'react';
import * as InfiniteScroll from 'react-infinite-scroller';
import styled from 'styled-components';
import { Item, SearchInput } from '../models/Search';

interface SearchResultProps {
  input: SearchInput;
  onLoad: (value: SearchInput) => void;
  hasMore: boolean;
  total: number;
  items: Item[];
}

const Container = styled.div`
  padding-top: 1rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
`;

const ItemWrapper = styled.ul`
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
`;

const Item = styled.li`
  margin: 1rem;
`;

const Poster = styled.img`
  width: 10rem;
  height: 14rem;
`;

const SearchResult = ({ items, total, onLoad, input, hasMore }: SearchResultProps) => {
  const loader = (
    <div key={0} style={{ clear: 'both' }}>
      Loading ...
    </div>
  );
  const loadMore = (page: number) => {
    // Need to figure out why infinate loop
    // tslint:disable-next-line:no-console
    console.log(page);
    onLoad({ ...input, page });
  };

  return (
    <Container>
      <InfiniteScroll pageStart={1} loadMore={loadMore} hasMore={hasMore} loader={loader}>
        <div>{`${total} titles`}</div>
        <ItemWrapper>
          {items.map(item => (
            <Item key={item.imdbID}>
              <Poster src={item.Poster} alt={item.Title} />
            </Item>
          ))}
        </ItemWrapper>
      </InfiniteScroll>
    </Container>
  );
};

export default SearchResult;

Here is the example: Edit my-app

Type Black to the Title input and press Search

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:23
  • Comments:33

github_iconTop GitHub Comments

46reactions
OmanFerrercommented, Aug 7, 2018

The problem is because you are not passing or controlling the property initialLoad correctly. This prop is true by default and every time componentDidUpdate() is executed, the method scrollListener() is executed too.

See code below:

  componentDidUpdate() {
    this.attachScrollListener();
  }

attachScrollListener() {
    .......
    if (this.props.initialLoad) {
      this.scrollListener();
    }
  }

I just passed the property initialLoad={this.state.initialLoad} to InfiniteScroll component and added this.setState({ initialLoad: false }); to loadMore function.

Hope can help you.

18reactions
didiselfcommented, Jun 16, 2018

So am I.

I use fetch request in loadMore . add await and change the hasMore option. solved.

Hope can help u.

Read more comments on GitHub >

github_iconTop Results From Across the Web

InfiniteScroll(react-infinite-scroller), loadMore function re ...
So here is the problem I am facing, as soon as I do a fetch call in the child component on the loadMore...
Read more >
React Query Load More Infinite Scroll Example - TanStack
An example showing how to implement Load More Infinite Scroll in React Query. ... an endpoint for getting projects data\nexport default (req, res)...
Read more >
Infinite loading with React Query - Daily Dev Tips
Loading more data with React Infinite Query permalink. Now that we have our initial data loaded, we want to have a button to...
Read more >
Infinite scroll vs load more button: which is better? | Publift
Lazy loading—also called on-demand loading—is a JavaScript feature that only calls for data from the server to be displayed on the page when ......
Read more >
Implementing Infinite Scroll with React Query and FlatList in ...
Add a handle method inside the HomeScreen component called loadMore . This function will be used on the FlatList prop called onEndReached ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found