A React component to make all your infinite scrolling problems go away with just 6 kB! An infinite-scroll that actually works and super-simple to integrate with redux and redux-saga!
import InfiniteScroll from 'react-redux-redux-saga-infinite-scroll';
const items = items.map((item, i)=>
<div className="item-wrapper">
{item}
</div>
)
<InfiniteScroll
currentPage= {currentPage}
total={total}
endText={endText}
loadMore={this.loadMoreAPICall}
>
{items}
</InfiniteScroll>
loadMoreAPICall = () => {
const paginationCount = 10 // Number of products per page to show
const currentPage = 1 // Data from API
this.props.getItemList(currentPage + 1, paginationCount); /* Get Request */
}
1. scrollerStyle = {
height: 'value',
overflow: 'auto'
}
2. endTextStyle = {
color: '#XXXXX',
padding: 'value',
textAlign: 'value',
}
3. spinnerIcon= '/url_of_icon.svg';
export const getItemList = (number, size) => {
return {
type: 'ITEM_LIST',
request: {
method: 'GET',
url: `/items?pageNumber=${number}&pageSize=${size}`,
}
};
};
function itemListReducer(state = {data: []}, action) {
switch (action.type) {
case success('ITEM_LIST'):
/* Write this part as your own for getting rid of duplicate data in state. */
const x = state.data && state.data.filter((data) => data && data.id === action.data.data[0] && action.data.data[0].id)
if(x[0] && x[0].id === action.data.data && action.data.data[0] && action.data.data[0].id) {
return state;
}
// Finally let's return the state
return {
...state,
data: [
...state.data,
...action.data.data
],
/* meta will be returning the data like, currentPage number, total number of items, etc. */
meta: {...state.meta, ...action.data.meta}
};
default:
return state
}
}
Reducer part remains the same, just add create rootSaga and attach the needed actions as per your setup.