React Collapse Component

4.0.0 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates a basic multi-panel collapse component with initial active state, change handler, and importing necessary styles. Shows deprecated `Panel` component usage as `items` prop was not fully introduced until the successor package.

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');
}

view raw JSON →