|
| 1 | +--- |
| 2 | +id: channel-list-infinite-scroll |
| 3 | +sidebar_position: 24 |
| 4 | +title: Channel List Infinite Scroll |
| 5 | +keywords: [channel list, infinite scroll] |
| 6 | +--- |
| 7 | + |
| 8 | +import GHComponentLink from '../_docusaurus-components/GHComponentLink'; |
| 9 | + |
| 10 | +This example demonstrates how to implement infinite scroll with existing SDK components. By default, the SDK's `ChannelList` component uses `LoadMorePaginator` to load more channels into the list. More channels are loaded every time the `LoadMoreButton` is clicked. The infinite scroll instead loads more channels based on the channel list container's scroll position. The request to load more channels is automatically triggered, once the scroller approaches the bottom scroll threshold of the container. |
| 11 | + |
| 12 | +## How to plug in the infinite scroll |
| 13 | + |
| 14 | +The SDK provides own [`InfiniteScroll`](https://github.com/GetStream/stream-chat-react/blob/master/src/components/InfiniteScrollPaginator/InfiniteScroll.tsx) component. This component implements the [`PaginatorProps`](https://github.com/GetStream/stream-chat-react/blob/master/src/types/types.ts) interface. As this interface is implemented by the [`LoadMorePaginator`](https://github.com/GetStream/stream-chat-react/blob/master/src/components/LoadMore/LoadMorePaginator.ts) too, we can just pass the `InfiniteScroll` into the `ChannelList` prop `Paginator`. |
| 15 | + |
| 16 | +```tsx |
| 17 | +import { |
| 18 | + ChannelList, |
| 19 | + InfiniteScroll, |
| 20 | +} from 'stream-chat-react'; |
| 21 | + |
| 22 | +<ChannelList filters={filters} sort={sort} options={options} |
| 23 | + Paginator={InfiniteScroll} |
| 24 | + showChannelSearch |
| 25 | +/> |
| 26 | +``` |
| 27 | + |
| 28 | +If you would like to adjust the configuration parameters like `threshold`, `reverse` (`PaginatorProps`) or `useCapture`, etc. (`InfiniteScrollProps`), you can create a wrapper component where these props can be set: |
| 29 | + |
| 30 | +```tsx |
| 31 | +import { |
| 32 | + ChannelList, |
| 33 | + InfiniteScroll, |
| 34 | + InfiniteScrollProps |
| 35 | +} from 'stream-chat-react'; |
| 36 | + |
| 37 | + |
| 38 | +const Paginator = (props: InfiniteScrollProps) => <InfiniteScroll {...props} threshold={50} />; |
| 39 | + |
| 40 | +... |
| 41 | +<ChannelList filters={filters} sort={sort} options={options} |
| 42 | + Paginator={Paginator} |
| 43 | + showChannelSearch |
| 44 | +/> |
| 45 | +``` |
| 46 | + |
| 47 | +Especially the `threshold` prop may need to be set as the default is 250px. That may be too soon to load more channels. |
| 48 | + |
| 49 | +## What to take into consideration |
| 50 | + |
| 51 | +For the infinite scroll to work, the element containing the `ChannelPreview` list has to be forced to display the scroll bar with the initial channel list load. This is achieved by: |
| 52 | + |
| 53 | +**1. adjusting the initial number of loaded channels** |
| 54 | + |
| 55 | +Set a reasonable number of channels to be initially loaded. If loading 10 channels leads to them being visible without having to scroll, then increase the number to e.g. 15: |
| 56 | + |
| 57 | +```tsx |
| 58 | +import type { ChannelOptions } from 'stream-chat'; |
| 59 | +const options: ChannelOptions = { state: true, presence: true, limit: 15 }; |
| 60 | +``` |
| 61 | + |
| 62 | +**2. adjusting the container height** |
| 63 | + |
| 64 | +You can change the container height so that not all channels are visible at once. You should target the container with class `.str-chat__channel-list-messenger-react__main` |
| 65 | + |
| 66 | +```css |
| 67 | +.str-chat__channel-list-messenger-react__main { |
| 68 | + max-height: 50%; |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | + |
0 commit comments