npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-paginated-list

v1.1.5

Published

A reusable paginated list component for React Native that handles infinite scrolling, API data fetching, and rendering components generically in a List

Downloads

1

Readme

react-native-paginated-list

This component renders a list of any custom UI component with data fetched from an API, efficiently managing pagination and infinite scrolling. Just provide your API endpoint and card component, and react-native-paginated-list takes care of data fetching, loading additional content as you scroll, customizable layouts, and displaying messages when no results are found.

The library centralizes pagination logic and is designed to be reusable across different scenarios, reducing repetitive code (following DRY—Don’t Repeat Yourself). It also adheres to the Single Responsibility Principle (SRP), focusing exclusively on handling pagination and data fetching, which simplifies both maintenance and extensibility.

You provide the API endpoint, the card UI component (e.g., product card, notification card, profile card), the number of items per page, and a few other props, and the component will:

  • Fetch data from the provided API endpoint.
  • Manage pagination efficiently.
  • Handle infinite scrolling (automatically fetching more data when the user scrolls near the end of the list).
  • Render the custom card component using a FlatList.
  • Display the list in a customizable number of columns.
  • Show a customizable message when no results are found or when an error occurs.

Features

  • API Data Fetching: Fetch data from a given API endpoint using pagination.
  • Infinite Scrolling: Automatically fetch more data when the user scrolls near the bottom of the list.
  • Custom Rendering: Pass a custom UI card component to render each item in the list.
  • Multiple Columns: Display the list items in a customizable number of columns.
  • Error and Empty State Handling: Show messages when there is an error or when no data is available.

Installation

To install the package, run:

npm install react-native-paginated-list

Usage

Here’s a simple 3 step example of how to use the PaginatedList component:

  1. First, you need to design a UI component that represents each item in your list. For example, if you are displaying products from an API, your component might show the product name and price. Here’s a simple example of a ProductCard component that displays the product’s name and price:
const ProductCard = ({ item }) => (
  <Text>{item.name}</Text>   
  <Text>{item.price}</Text>
);
  1. Your API endpoint should return data in a format like this, where the data array contains a list of products:
    {
        "data": [
            { 
                "id": 1, 
                "name": "Product 1" , 
                "price" :10  
            }, 
            { 
                "id": 2, 
                "name": "Product 2" , 
                "price" : 15  
            }
        ],
        "meta": {
            "pagination": {
                "page": 1,
                "pageCount": 1
            }
        }
    } 
  • data : The array of items that will be displayed in the list.
  • meta.pagination.page : The current page number, used to determine the next page to fetch.
  • meta.pagination.pageCount : The total number of pages, used to determine when to stop fetching additional data.

Important Note" react-native-paginated-list expects the backend to return data in the specified format, particularly the meta section. This section is crucial as it provides the pagination details used by the component to calculate the next page for infinite scrolling.

  • It is also important to have a unique id as part of the item being fetched from backend,
  1. Use PaginatedList and pass the props (details below), wrapping it in a QueryClientProvider (required by react-query, which is used by the PaginatedList component)
import React from 'react';
import { PaginatedList } from 'react-native-paginated-list';
import { QueryClient, QueryClientProvider } from 'react-query';
import ProductCard from './components/ProductCard';

// Create a QueryClient instance
const queryClient = new QueryClient();
  
const ProductsScreen = () => (
  // Wrap your component tree with QueryClientProvider
  <QueryClientProvider client={queryClient}>
    <PaginatedList
      CardComponent={ProductCard}
      endpoint="https://api.example.com/products"
      itemsPerPage  
      collectionPath="products"
      refetchKey
      columns={2}
      emptyMessageEntity = 'items' 
      axiosInstance
      loaderColor="lightgrey"
    />
  </QueryClientProvider>
);

export default ProductsScreen;

Props

| Prop Name | Type | Default | Description | |----------------------|-------------------|-------------|-------------| | CardComponent | Component | Required| The component used to render each item in the list. | | endpoint | string | Required| The API endpoint to fetch paginated data from. | | collectionPath | string | 'data' | Path in the response where the list of items is located. | | itemsPerPage | number | 30 | Number of items to fetch per page. | | refetchKey | number | 0 | Key used to trigger refetching the data. | | columns | number | 1 | Number of columns in the FlatList. | | loaderColor | string | 'lightgrey' | Color of the loading spinner (ActivityIndicator). | | emptyMessageEntity | string | 'Items' | The entity name to use in the "No X found" message. | | axiosInstance | object | defaultAxios | Optional custom Axios instance for API calls. |

Roadmap

  • Make the pagination params flexible
  • Add querying filter params
  • Pull to refresh functionality.
  • More customizable styling of empty message and error message.
  • More battle testing.
  • Probably Horizontal/Vertical scrolling prop?
  • Other ideas are welcome.