loadMore function get called infinitely
See original GitHub issueI 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;
Type Black
to the Title
input and press Search
Issue Analytics
- State:
- Created 5 years ago
- Reactions:23
- Comments:33
Top 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 >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
The problem is because you are not passing or controlling the property
initialLoad
correctly. This prop is true by default and every timecomponentDidUpdate()
is executed, the methodscrollListener()
is executed too.See code below:
I just passed the property
initialLoad={this.state.initialLoad}
to InfiniteScroll component and addedthis.setState({ initialLoad: false });
to loadMore function.Hope can help you.
So am I.
I use fetch request in
loadMore
. addawait
and change thehasMore
option. solved.Hope can help u.