React Collapse Component
rc-collapse is a foundational React UI component for creating collapsible panels and accordions. This package, at version 4.0.0, is effectively the final major release under the `rc-collapse` name. Development and active maintenance have since transitioned to the `@rc-component/collapse` package, which continues to evolve the component's features and API. While `rc-collapse@4.0.0` is stable and functional for its specified React peer dependencies (`>=16.9.0`), new features, bug fixes, and long-term support are primarily focused on the successor package. It provides basic collapse functionality with options for accordion mode, custom expand icons, and panel content destruction, but its API for defining panels via `Collapse.Panel` components is superseded by an `items` prop in the newer `@rc-component/collapse` package.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'Panel')
cause Attempting to destructure `Panel` from `rc-collapse` when `Collapse` was imported as a named export or incorrectly.fixEnsure `Collapse` is imported as a default export: `import Collapse from 'rc-collapse';` and then access `Panel` as a property: `const { Panel } = Collapse;` -
Module not found: Can't resolve 'rc-collapse/assets/index.css' in '...'
cause The required CSS file for `rc-collapse` styles is not being imported or is imported with an incorrect path.fixAdd `import 'rc-collapse/assets/index.css';` to your main application file or the component using `rc-collapse`. -
Warning: Each child in a list should have a unique "key" prop.
cause When using `Collapse.Panel` components, each `Panel` must have a unique `key` prop, especially when used in lists or dynamically generated. The `key` prop is also used by `activeKey` to identify panels.fixEnsure every `<Panel>` component has a unique and stable `key` prop, e.g., `<Panel key="1" header="Panel 1">...</Panel>`.
Warnings
- breaking The `rc-collapse` package, particularly at version 4.0.0, is effectively succeeded by the `@rc-component/collapse` package. While `rc-collapse@4.0.0` is a stable release, all future development, bug fixes, and new features will occur under the new package name. Migrating to `@rc-component/collapse` is highly recommended for ongoing projects.
- deprecated The use of `<Collapse.Panel>` components as children to define panels is deprecated starting from `rc-collapse@4.0.0`. The successor package, `@rc-component/collapse`, primarily uses an `items` prop on the `Collapse` component for defining panel content and properties. While `Panel` components might still function in `v4.0.0`, they are not the forward-looking approach.
- gotcha CSS styles are not automatically included and must be imported separately. For `rc-collapse`, the styles are located at `rc-collapse/assets/index.css`.
- gotcha The `Collapse` component is a default export, meaning it should be imported without curly braces. Its `Panel` sub-component is exposed as a static property, not a named export.
Install
-
npm install rc-collapse -
yarn add rc-collapse -
pnpm add rc-collapse
Imports
- Collapse
import { Collapse } from 'rc-collapse';import Collapse from 'rc-collapse';
- Panel (via Collapse)
import { Panel } from 'rc-collapse';import Collapse from 'rc-collapse'; const { Panel } = Collapse; - CSS Styles
import 'rc-collapse/assets/index.css';
Quickstart
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import Collapse from 'rc-collapse';
import 'rc-collapse/assets/index.css';
const { Panel } = Collapse; // Panel is a static property, but deprecated in v4.0.0 in favor of 'items' prop for the successor package
function App() {
const [activeKey, setActiveKey] = useState(['1']);
const handleChange = (keys) => {
setActiveKey(keys);
console.log('Active keys changed:', keys);
};
return (
<div style={{ maxWidth: '600px', margin: '20px auto' }}>
<h1>RC Collapse Example (v4.0.0)</h1>
<Collapse
accordion={false}
activeKey={activeKey}
onChange={handleChange}
destroyInactivePanel={true}
>
<Panel header="This is Panel 1" key="1">
<p>Content for the first panel.</p>
<p>You can put any React nodes here.</p>
</Panel>
<Panel header="This is Panel 2" key="2">
<p>Content for the second panel.</p>
</Panel>
<Panel header="This is Panel 3 (Accordion Mode Disabled)" key="3">
<p>Content for the third panel.</p>
<p>Since accordion is false, multiple panels can be open.</p>
</Panel>
</Collapse>
</div>
);
}
const container = document.getElementById('root');
if (container) {
const root = ReactDOM.createRoot(container);
root.render(<App />);
} else {
console.error('Root element not found');
}