React Tabs Component
rc-tabs is a foundational React UI component designed to provide a highly flexible and accessible tab interface. It is part of the `react-component` ecosystem, focusing on delivering core functionality without imposing specific styling, allowing developers to integrate it seamlessly into various design systems. The current stable version is 15.7.0, with regular updates that typically occur weekly or bi-weekly, addressing bug fixes, accessibility enhancements, and minor feature additions. Its key differentiators include built-in keyboard navigation for improved accessibility, extensive customization options via `prefixCls`, `className`, `style`, and `renderTabBar` props, and a commitment to being a lightweight, unstyled component. This makes `rc-tabs` an ideal choice for projects requiring a robust tab solution that prioritizes control over visual presentation.
Common errors
-
Warning: Each child in a list should have a unique "key" prop.
cause Elements within the `items` array, or custom rendered tabs, are missing a unique `key` prop, which React requires for efficient list reconciliation and preventing unexpected behavior.fixEnsure every object in the `items` array has a unique `key` string property. If using a custom `renderTabBar`, explicitly assign unique `key` props to each tab element you render. -
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
cause This error typically occurs when the `activeKey` prop is used without properly updating its value in the `onChange` handler, causing an uncontrolled re-render loop, or due to other state management issues within the Tabs children.fixIf `activeKey` is used, ensure the `onChange` handler updates the state that `activeKey` depends on. Verify no other component within the Tabs tree is causing uncontrolled re-renders by calling `setState` directly or indirectly in a render cycle.
Warnings
- breaking The `destroyInactiveTabPane` prop has been renamed to `destroyOnHidden` for clarity and consistency. Using the old prop name will no longer have an effect and should be updated.
- gotcha When managing active tabs, distinguish between `defaultActiveKey` for uncontrolled components (initial state only) and `activeKey` for controlled components (state managed by a parent). Misusing `activeKey` without an `onChange` handler to update its value will prevent tab changes.
- gotcha The API documentation lists both `tabPosition` and `tabBarPosition` props with seemingly identical functionalities (`'left' | 'right' | 'top' | 'bottom'`). While both might work, `tabPosition` is more commonly used in examples and is the recommended approach for clarity.
Install
-
npm install rc-tabs -
yarn add rc-tabs -
pnpm add rc-tabs
Imports
- Tabs
import { Tabs } from 'rc-tabs'; const Tabs = require('rc-tabs');import Tabs from 'rc-tabs';
- TabItem
import type { TabItem } from 'rc-tabs'; - TabsProps
import type { TabsProps } from 'rc-tabs';
Quickstart
import Tabs from 'rc-tabs';
import React from 'react';
import ReactDOM from 'react-dom/client';
const callback = (key: string) => {
console.log(`Active tab: ${key}`);
};
const items = [
{
key: '1',
label: 'Google',
children: (
<div className="text-xl">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
),
},
{
key: '2',
label: <p>Amazon</p>,
children:
'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...',
disabled: true,
},
{
key: '3',
label: <p>Twitter</p>,
children: (
<div>
"There is no one who loves pain itself, who seeks after it and wants to have it, simply
because it is pain..."
</div>
),
},
];
// Ensure a root element with id="root" exists in your public/index.html
const rootElement = document.getElementById('root');
if (rootElement) {
const root = ReactDOM.createRoot(rootElement);
root.render(
<React.StrictMode>
<Tabs
tabPosition="bottom"
items={items}
defaultActiveKey="1"
className="md:w-[70%] w-full mx-auto p-2 border-0"
onChange={callback}
style={{ color: 'yellow', border: '1px solid #eee', padding: 10 }}
/>
</React.StrictMode>
);
} else {
console.error('Root element with id "root" not found in the document.');
}