Skip to content

Repository files navigation

react-carousel-query

NPM

TypeScript

StatementsBranchesFunctionsLines
StatementsBranchesFunctionsLines
  • 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

Screenshot

Installation

Requirements: React 16.8.0 or higher

npm install react-carousel-query

Props

PropTypeDefaultDescription
renderItemfunctionrequiredRender function for each slide. Receives { item } as argument. You can render just an img or any other React element.
getDatafunctionrequiredAsync function to fetch items. Receives { offset, cursor, limit } and must return { total, items }. Each item must have an id property (string or number).
fetchStepnumber3Number of items requested per fetch call. Data is fetched preemptively as the user navigates, ensuring smooth transitions.
hideIndexbooleanfalseHide the index badge in the top right corner.
showArrowsbooleanfalseShow navigation arrows. Also enabled when renderArrow is provided.
renderArrowfunctionundefinedCustom render function for arrows.
renderBadgefunctionundefinedCustom render function for the index badge.

Usage

Basic Example (Offset-based pagination)

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}/>)

Cursor-based pagination

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.

Setup

npm install

How to run

npm run dev

How to build

npm run build

How to test

npm test

Contributions

Contributions 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.

Documentation

Run Storybook for interactive documentation:

npm run storybook

About

A infinite carousel react component for mobile and desktop that can handle the api calls for you.

Resources

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages