Skip to content

usePagination

usePagination is a Hook designed for traditional paginated list scenarios, handling page number, total count, loading state and other logic, allowing you to focus on rendering tables or lists.

Basic Usage

typescript
import { usePagination } from '@yh-ui/request'

interface UserListResult {
  list: User[]
  total: number
}

const {
  current, // Current page number
  pageSize, // Items per page
  total, // Total count
  totalPages, // Total pages
  data, // ShallowRef<UserListResult | undefined>
  loading,
  pagination // Pagination method collection
} = usePagination<UserListResult>(
  // service: (page, pageSize) => Promise<RequestResponse<T>>
  (page, size) =>
    request.get('/api/users', {
      params: { page, pageSize: size }
    }),
  {
    defaultPagination: {
      current: 1,
      pageSize: 10
    }
  }
)

Template example (with YH-UI Pagination):

vue
<template>
  <yh-table :data="data?.list || []" />

  <yh-pagination
    v-model:current="current"
    v-model:page-size="pageSize"
    :total="total"
    @change="pagination.loadPage"
  />
</template>

Return Values

typescript
const result = usePagination<TData, TParams>(service, options)
FieldTypeDescription
currentRef<number>Current page
pageSizeRef<number>Items per page
totalRef<number>Total records
totalPagesRef<number>Total pages
dataShallowRef<TData | undefined>Response data (usually contains list and total)
loadingRef<boolean>Loading
refreshingRef<boolean>Refreshing
loadingMoreRef<boolean>Loading more (used with other Hooks)
errorShallowRef<unknown>Error
paramsRef<TParams>Extra params
noMoreRef<boolean>Whether there's no more data
pagination.loadPage(page)(page: number) => voidLoad specified page
pagination.nextPage()() => voidNext page
pagination.prevPage()() => voidPrevious page
pagination.firstPage()() => voidFirst page
pagination.lastPage()() => voidLast page
pagination.refresh()() => voidRefresh current page
pagination.setPageSize(size)(size: number) => voidChange items per page and auto refresh (non-manual mode)
pagination.setTotal(total)(total: number) => voidManually set total count

Options

The first argument to usePagination(service, options) is the request function service(page, pageSize, ...args); the second is the options object:

typescript
interface UsePaginationOptions<TData, TParams extends unknown[]> {
  defaultPagination?: PaginationOptions // Default pagination { current, pageSize }
  request?: Request // Custom request instance (reserved)
  onSuccess?: (data: TData, params: TParams) => void
  onError?: (error: unknown, params: TParams) => void
  onFinally?: (params: TParams) => void
  manual?: boolean // Manual first load
  defaultParams?: TParams // Extra params besides page and pageSize (e.g. keyword)
}

Common usage:

typescript
const { current, pageSize, total, data, loading, pagination } = usePagination(
  (page, size, keyword: string) =>
    request.get('/api/users', { params: { page, pageSize: size, keyword } }),
  {
    defaultPagination: { current: 1, pageSize: 20 },
    defaultParams: [''], // Third param keyword
    onSuccess: (res) => {
      console.log('List loaded successfully', res)
    }
  }
)

The first two parameters of service are fixed as (current, pageSize), additional params can be passed through defaultParams / manually modifying params.

Manual Mode

typescript
const state = reactive({
  keyword: ''
})

const { current, pageSize, total, data, loading, pagination, params } = usePagination<
  UserListResult,
  [number, number, string]
>(
  (page, size, keyword) => request.get('/api/users', { params: { page, pageSize: size, keyword } }),
  {
    manual: true,
    defaultPagination: { current: 1, pageSize: 10 },
    defaultParams: [1, 10, ''] // [page, size, keyword]
  }
)

// Manually load
const load = () => {
  params.value = [current.value, pageSize.value, state.keyword]
  pagination.refresh()
}

Difference from useRequest

  • useRequest: More generic, suitable for various single requests / debounce / throttle / SWR, etc.
  • usePagination: Specifically handles pagination logic, more suitable for typical "list + pagination" scenarios

Released under the MIT License.