loadmore gets called infinitely
See original GitHub issueI 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
)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:15
- Comments:14 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Set a lock like
loading
to prevent infinite loopsFor 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