| Statements | Branches | Functions | Lines |
|---|---|---|---|
- Lightweight React component with minimal footprint
- Infinite carousel with automatic pagination management
- Fetches data on demand as users navigate through slides
- Zero external dependencies (only React and classnames)
- Fully customizable slide, arrow, and badge rendering
- Optimized for performance
- Touch and mouse swipe support for mobile and desktop
- TypeScript declarations included
Working demo here
Requirements: React 16.8.0 or higher
npm install react-carousel-query| Prop | Type | Default | Description |
|---|---|---|---|
renderItem | function | required | Render function for each slide. Receives { item } as argument. You can render just an img or any other React element. |
getData | function | required | Async function to fetch items. Receives { offset, cursor, limit } and must return { total, items }. Each item must have an id property (string or number). |
fetchStep | number | 3 | Number of items requested per fetch call. Data is fetched preemptively as the user navigates, ensuring smooth transitions. |
hideIndex | boolean | false | Hide the index badge in the top right corner. |
showArrows | boolean | false | Show navigation arrows. Also enabled when renderArrow is provided. |
renderArrow | function | undefined | Custom render function for arrows. |
renderBadge | function | undefined | Custom render function for the index badge. |
importReactCarouselQueryfrom'react-carousel-query'import'react-carousel-query/styles.css'// Required for stylesconstgetData=async({ offset, limit })=>{constresponse=awaitfetch(`https://api.example.com/items?offset=${offset}&limit=${limit}`)const{ data }=awaitresponse.json()return{total: data.total,// Total number of items availableitems: data,// Each item must have an `id` property (string | number)}}constApp=()=>(<ReactCarouselQueryrenderItem={({ item })=><imgsrc={item.imgSrc}alt={item.name}/>}getData={getData}/>)To use cursor-based pagination, return nextCursor in your response. The component auto-detects the pagination mode:
constgetData=async({ cursor, limit })=>{consturl=cursor
? `https://api.example.com/items?cursor=${cursor}&limit=${limit}`
: `https://api.example.com/items?limit=${limit}`constresponse=awaitfetch(url)const{ data, nextCursor, totalCount }=awaitresponse.json()return{items: data,
nextCursor,// null when there are no more pagestotal: totalCount,// optional - will be inferred from items if not provided}}For a complete working example, check out our demo code.
npm installnpm run devnpm run buildnpm testContributions are welcome. Just open a PR and feel free to contact me :-).
You can also start looking into ope issues, specially the ones with good first issue label.
Run Storybook for interactive documentation:
npm run storybook
