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 gets called infinitely

See original GitHub issue

I have a tree component and I’m trying to display 100 items at a time. I don’t have any ajax calls to make to load next set of item.

Here’s what the code looks like:

class Tree extends Component {
  static propTypes = {
    searchModeOn: PropTypes.bool
  }

  constructor(props) {
    super(props)

    this.pageSize = 100

    this.computeInstanceProps(props)

    this.state = {
      items: this.allVisibleNodes.slice(0, this.pageSize),
      hasMore: this.hasMore()
    }
  }

  componentWillReceiveProps = nextProps => {
    this.computeInstanceProps(nextProps)
    this.setState({ items: this.allVisibleNodes.slice(0, this.pageSize), hasMore: this.hasMore() })
  }

  computeInstanceProps = props => {
    this.allVisibleNodes = props.items            // props.items is the list of all items. 
    this.totalPages = this.allVisibleNodes.length / this.pageSize
    this.currentPage = 1
  }

  hasMore = () => {
    return this.currentPage <= this.totalPages
  }

  loadMore = page => {
    const start = this.pageSize * (page - 1)
    const nextItems = this.allVisibleNodes.slice(start, start + this.pageSize)
    this.currentPage = page
    this.setState({ items: nextItems, hasMore: this.hasMore() })
  }

  render() {
    console.log('re', this.props, this.state)
    const { searchModeOn } = this.props

    return (
      <ul className={`root ${searchModeOn ? 'searchModeOn' : ''}`}>
        <InfiniteScroll
          pageStart={0}
          initialLoad
          loadMore={this.loadMore}
          hasMore={this.state.hasMore}
          loader={
            <div className="loader" key={0}>
              Loading ...
            </div>
          }
          useCapture
          useWindow={false}
        >
          {this.state.items}
        </InfiniteScroll>
      </ul>
    )
  }
}

I’ve tried various combinations of initialLoad, hasMore, loadMore but either it stops after first load or keeps firing loadMore until it has reached the last page (the above code will keep firing loadMore)

ezgif com-optimize

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:15
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
lyyourccommented, Feb 25, 2019

Set a lock like loading to prevent infinite loops

async function loadMore() {
  if (this.state.loading) {
    return 
  }

  this.setState({ loading: true })
  await fetchUser()
  this.setState({ loading: false)
}
2reactions
raymondshinercommented, Aug 3, 2020

For those using functional react to accomplish this, you will also have to set awaits on your setState methods as react hooks for useState are asynchronous

const onLoadMore = async () => {
    if (listIsLoading) {
      return;
    }
    await setListIsLoading(true);
    await fetchMore();
    await setListIsLoading(false);
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

loadMore function get called infinitely #163 - GitHub
I use React-Redux to pass loadMore and hasMore Code: import * as React from 'react'; import * as InfiniteScroll from 'react-infinite-scroller'; ...
Read more >
“react-infinite-scroll-component” Stopped working after one ...
“react-infinite-scroll-component” Stopped working after one call (loadMore only gets called once) · Ask Question. Asked 1 year, 7 months ago.
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 >
react-infinite-scroller - NPM Package Overview - Socket.dev
Start using Socket to analyze react-infinite-scroller and its 1 dependencies ... If you experience double or non-stop calls to your loadMore ...
Read more >
ion-infinite-scroll Action Component - Ionic Framework
The ion-infinite-scroll component calls an action to be performed when the ... use the value of 10% for the infinite output event to...
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