React Pagination Component
rc-pagination is a highly customizable and flexible React UI component for building pagination controls. While version 5.1.0 is the current stable release under this package name, the project has formally transitioned and is actively maintained as the `@rc-component/pagination` package. This library is renowned for its extensive API, offering deep customization options for appearance and behavior, including support for internationalization, quick page jumpers, page size changers, and custom renderers for individual page items. It serves as the foundational pagination logic for prominent UI libraries such as Ant Design, emphasizing accessibility and performance. Developers initiating new projects or upgrading should use `@rc-component/pagination` for the latest features and ongoing support, as `rc-pagination` is effectively superseded by the renamed package.
Common errors
-
Module not found: Can't resolve 'rc-pagination'
cause The package `rc-pagination` is not installed or the import path is incorrect. Alternatively, if migrating, the package may have been renamed.fixEnsure the package is installed via `npm install rc-pagination` or `yarn add rc-pagination`. If you intend to use the newer version, install `@rc-component/pagination` instead and update your import statements. -
Property 'selectComponentClass' does not exist on type 'PaginationProps'.
cause You are attempting to use the `selectComponentClass` prop, which was removed in `rc-pagination` v5.0.0 and replaced by `sizeChangerRender`.fixUpdate your code to use the `sizeChangerRender` prop for customizing the page size changer. Consult the v5 API documentation for proper usage. -
TypeError: Cannot read properties of undefined (reading 'render') or Invariant Violation: Target container is not a DOM element.
cause You might be using an older `ReactDOM.render` API without a valid DOM container, or calling it incorrectly in a modern React setup.fixFor modern React, ensure you have a valid DOM element (e.g., `<div id="root"></div>`) and use `ReactDOM.createRoot(container).render(<App />);` for rendering your application.
Warnings
- breaking The `rc-pagination` package has been renamed and is now primarily maintained under the `@rc-component/pagination` scope. While `rc-pagination` v5.1.0 is stable, new features and ongoing development will occur in the renamed package. Future migrations should target `@rc-component/pagination`.
- breaking The `selectComponentClass` prop was removed in version 5.0.0 and replaced by `sizeChangerRender`. If you were using `selectComponentClass` to customize the page size changer, your component will break.
- gotcha The component's essential visual styles are not bundled with the JavaScript and require a separate CSS import. Neglecting this will result in an unstyled or poorly rendered pagination component.
- gotcha Using `rc-pagination` requires `React` and `ReactDOM` peer dependencies of version `16.9.0` or higher. Older React versions may lead to runtime errors or unexpected behavior.
Install
-
npm install rc-pagination -
yarn add rc-pagination -
pnpm add rc-pagination
Imports
- Pagination
import { Pagination } from 'rc-pagination';import Pagination from 'rc-pagination';
- PaginationProps
import type { PaginationProps } from 'rc-pagination'; - Pagination Styles
import 'rc-pagination/assets/index.css';
- Pagination (CommonJS)
const { Pagination } = require('rc-pagination');const Pagination = require('rc-pagination');
Quickstart
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import Pagination from 'rc-pagination';
import type { PaginationProps } from 'rc-pagination';
import 'rc-pagination/assets/index.css'; // Don't forget to import styles!
function App() {
const [current, setCurrent] = useState(1);
const totalItems = 100;
const pageSize = 10;
const onChange: PaginationProps['onChange'] = (page, pageSize) => {
console.log(`Page: ${page}, PageSize: ${pageSize}`);
setCurrent(page);
};
return (
<div style={{ padding: '20px' }}>
<h1>rc-pagination Example</h1>
<Pagination
onChange={onChange}
current={current}
total={totalItems}
pageSize={pageSize}
showTotal={(total, range) => `${range[0]}-${range[1]} of ${total} items`}
showSizeChanger
pageSizeOptions={['10', '20', '30']}
showQuickJumper={{ goButton: true }}
locale={{ jump_to: 'Go to', page: 'Page' }}
/>
<p>Current Page: {current}</p>
<p>Total Items: {totalItems}</p>
</div>
);
}
const container = document.getElementById('root');
if (container) {
const root = ReactDOM.createRoot(container);
root.render(<App />);
} else {
console.error("Root element not found. Please ensure an element with id 'root' exists in your HTML.");
}