@aboutbits/react-pagination 中文文档教程

发布于 3年前 浏览 15 项目主页 更新于 3年前

React Pagination

npm 包license

这个包包括 React 的分页挂钩。 钩子支持在本地保存查询和分页值 状态或在浏览器 URL 中。

Table of content

Usage

首先,您必须安装包:

npm install @aboutbits/react-pagination

其次,您可以使用 useQueryAndPagination 挂钩。 这个包实现了这个钩子的 3 个版本:

  • In Memory: Use this hook where you don't want to modify browser history. e.g. Dialogs
  • React Router: Use this hook if you want to keep track of the state in the URL and your project is using React Router.
  • NextJS Router: Use this hook if you want to keep track of the state in the URL and your project is using NextJS.

useQueryAndPagination

这个钩子支持查询参数和分页的组合,并管理查询参数值的状态和 分页值。

The hook supports following configuration parameter object:

valuetypedefaultdescription
indexTypeIndexTypeIndexType.ZERO_BASEDIt defines whether the pagination is zero or one based.
pageSizenumber15Page size of the pagination.
defaultQueryParameters/Record{}It defines the default value for each query parameter. This is used to remove a query parameter from the URL and also to clear the query.

The hook returns the following object:

valuetypedescription
queryParametersobjectvalues of your query parameters
pagenumbervalue of the current page
sizenumbermax elements in a single page
actionsobjectobject with 3 functions: updateQuery, setPage, clear

Example usage with NextJS

import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/nextRouterPagination'

const users = [
    'Alex', 'Simon', 'Natan', 'Nadia', 'Moritz', 'Marie'
]

function UserList() {
    const { page, size, queryParameters, actions } = useQueryAndPagination({pageSize: 2})

    return (
        <div>
            <input onChange={(value) => actions.updateQuery({search: value})}/>
            <button onClick={() => actions.clear()}>Clear Input</button>
            <select onSelect={(value) => actions.setPage(value)}>
                <option value={0}>First Page</option>
                <option value={1}>Second Page</option>
            </select>

            <ul>
                {users.filter(user => user.startsWith(queryParameters.search))
                    .slice(page, page + size)
                    .map(user => <li>{user}</li>)}
            </ul>
        </div>
    )
}

Supported implementations

这个包包括上述钩子的 3 种不同实现。

In Memory Pagination

如果您想跟踪内存中的分页,请使用此分页挂钩。 这对于对话框来说非常方便。

import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/inMemoryPagination'

React-Router based pagination

这些是使用 React Router 进行路由的应用程序的特定挂钩。

import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/reactRouterPagination'

NextJS Router based pagination

这些是使用 NextJS Router 的应用程序的特定挂钩 用于路由。

import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/nextRouterPagination'

Build & Publish

要发布包,请提交所有更改并将它们推送到 main。 然后在本地运行以下命令之一:

npm version patch
npm version minor
npm version major

Information

About Bits 是一家位于意大利南蒂罗尔的公司。 您可以找到更多关于我们的信息 在我们的网站

Support

如需支持,请联系 info@aboutbits.it

Credits

License

麻省理工学院许可证(麻省理工学院)。 请参阅许可文件了解更多信息。

React Pagination

npm packagelicense

This package includes pagination hooks for React. The hooks support saving the query and pagination values in local state or in the browser URL.

Table of content

Usage

First, you have to install the package:

npm install @aboutbits/react-pagination

Second, you can make use of the useQueryAndPagination hook. This package implements 3 versions of this hook:

  • In Memory: Use this hook where you don't want to modify browser history. e.g. Dialogs
  • React Router: Use this hook if you want to keep track of the state in the URL and your project is using React Router.
  • NextJS Router: Use this hook if you want to keep track of the state in the URL and your project is using NextJS.

useQueryAndPagination

This hook supports the combination of query parameters and pagination and manages the state of the query parameter values and the pagination values.

The hook supports following configuration parameter object:

valuetypedefaultdescription
indexTypeIndexTypeIndexType.ZERO_BASEDIt defines whether the pagination is zero or one based.
pageSizenumber15Page size of the pagination.
defaultQueryParameters/Record{}It defines the default value for each query parameter. This is used to remove a query parameter from the URL and also to clear the query.

The hook returns the following object:

valuetypedescription
queryParametersobjectvalues of your query parameters
pagenumbervalue of the current page
sizenumbermax elements in a single page
actionsobjectobject with 3 functions: updateQuery, setPage, clear

Example usage with NextJS

import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/nextRouterPagination'

const users = [
    'Alex', 'Simon', 'Natan', 'Nadia', 'Moritz', 'Marie'
]

function UserList() {
    const { page, size, queryParameters, actions } = useQueryAndPagination({pageSize: 2})

    return (
        <div>
            <input onChange={(value) => actions.updateQuery({search: value})}/>
            <button onClick={() => actions.clear()}>Clear Input</button>
            <select onSelect={(value) => actions.setPage(value)}>
                <option value={0}>First Page</option>
                <option value={1}>Second Page</option>
            </select>

            <ul>
                {users.filter(user => user.startsWith(queryParameters.search))
                    .slice(page, page + size)
                    .map(user => <li>{user}</li>)}
            </ul>
        </div>
    )
}

Supported implementations

This package includes 3 different implementations of the above hook.

In Memory Pagination

Use this pagination hook if you want to keep track of the pagination in memory. This is very handy for dialogs.

import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/inMemoryPagination'

React-Router based pagination

These are specific hooks for applications that use React Router for routing.

import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/reactRouterPagination'

NextJS Router based pagination

These are specific hooks for applications that use NextJS Router for routing.

import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/nextRouterPagination'

Build & Publish

To publish the package commit all changes and push them to main. Then run one of the following commands locally:

npm version patch
npm version minor
npm version major

Information

About Bits is a company based in South Tyrol, Italy. You can find more information about us on our website.

Support

For support, please contact info@aboutbits.it.

Credits

License

The MIT License (MIT). Please see the license file for more information.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文