1.0.0-beta.17 • Published 1 year ago

use-buffered-pagination v1.0.0-beta.17

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

use-buffered-pagination

React hook to achieve memory-efficient buffered pagination

Showcase

function Component() {
    const [searchQuery, setSearchQuery] = useState("");

    const pagination = useBufferedPagination<number>({
        query: searchQuery, // a buffer will be created for every different query
        count: undefined, // if not set, will be calculated after fetching
        page: 0, // the initial page for the current query
        pageSize: 0, // the initial page size
        pageBufferRadius: 1, // the radius of the buffer
        queryBackstackSize: 1, // how many query buffers can be retain at once
        async fetch(params) {
            // the first (default) range to fetch
            params.range;
            // the first range with padding
            params.paddedRange;
            // the currently known items count
            params.count;
            // negative if the user is paginating backwards 
            // and positive when paginating forwards 
            params.direction;
            // the list of all the ranges to be fetch (if possible)
            params.ranges;

            const [dataOffset, data] = myFetchFunction(params);

            return {
                remaining, // the remaining items, can be omitted or be a negative value
                slices: [
                    new BufferSlice(dataOffset, data)
                    // it is allowed to pass more than one slice
                    // the slices will be joind and arranged 
                    // depending on the offset of each slice
                ]
            };
        }
    });

    // page control
    pagination.page;
    pagination.setPage;
    pagination.pageSize;
    pagination.setPageSize;

    // data
    pagination.count;
    pagination.pageCount;
    pagination.loading;
    pagination.sequential; // if all the items is available
    pagination.absence; // the absent ranges in the data
    pagination.data;

    pagination.fetch(/* ... */); // force the hook to fetch some range
    pagination.insert(/* ... */); // insert data directly to the hook

    return <></>;
}
1.0.0-beta.17

1 year ago

1.0.0-beta.16

1 year ago

1.0.0-beta.15

1 year ago

1.0.0-beta.14

1 year ago

1.0.0-beta.13

1 year ago

1.0.0-beta.12

1 year ago

1.0.0-beta.11

1 year ago

1.0.0-beta.10

1 year ago

1.0.0-beta.9

1 year ago

1.0.0-beta.8

1 year ago

1.0.0-beta.7

1 year ago

1.0.0-beta.6

1 year ago

1.0.0-beta.5

1 year ago

1.0.0-beta.4

1 year ago

1.0.0-beta.3

1 year ago

1.0.0-beta.2

1 year ago

1.0.0-beta.1

1 year ago

1.0.0-beta.0

1 year ago