rc-dropdown UI Component
rc-dropdown is a fundamental, unstyled UI component for React applications, providing the core logic and accessibility features for dropdown menus, tooltips, and contextual overlays. It is currently at stable version 4.2.1 and is part of the extensive react-component library ecosystem, known for building highly customizable and performant UI primitives. While it offers essential dropdown functionalities like visibility control, placement, and event handling, it intentionally leaves styling and complex menu rendering to the consumer or higher-level UI libraries like Ant Design, which heavily utilize rc-dropdown internally. The project maintains an active development pace with regular updates, including refactors and minor feature enhancements, but major version bumps can introduce significant internal changes.
Common errors
-
Module not found: Can't resolve 'rc-dropdown' in '...' (webpack error)
cause The `rc-dropdown` package is not installed in the project's dependencies or there is a typo in the import path.fixInstall the package using your package manager: `npm install rc-dropdown` or `yarn add rc-dropdown`. Double-check the import statement for correct spelling. -
TypeError: Dropdown is not a function (or constructor) / (0, _rcDropdown.default) is not a function
cause This error typically occurs when attempting to import the `Dropdown` component using incorrect ES Module syntax for a default export, or when mixing CommonJS `require` with ES Modules.fixEnsure you are using the correct ES Module default import: `import Dropdown from 'rc-dropdown';`. Avoid `const Dropdown = require('rc-dropdown');` in ES Module contexts or `import { Dropdown } from 'rc-dropdown';` as the component is a default export. -
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause This is a common error in React indicating a mismatch in `react` or `react-dom` versions, or multiple instances of React being loaded. `rc-dropdown` has a strict peer dependency on `react@>=16.11.0`.fixVerify that your project's `react` and `react-dom` versions satisfy `rc-dropdown`'s peer dependency requirement (>=16.11.0). Use `npm ls react` and `npm ls react-dom` to inspect versions. If multiple React instances are present, investigate your bundling configuration to deduplicate `react`.
Warnings
- breaking Version `4.0.0` introduced significant internal refactors, specifically "Remove all coupling code" and migrations related to `@rc-component/trigger`. While specific API breaks were not always explicitly detailed in commit messages, these changes could affect custom implementations relying on internal component structure, specific DOM element IDs, or generated CSS class names. Thorough testing is recommended when upgrading from `v3.x` to `v4.x`.
- gotcha The `react-component` ecosystem is undergoing a migration, with new components and potentially future versions of existing components being published under the `@rc-component/` npm scope (e.g., `@rc-component/dropdown`). While `rc-dropdown` is currently active at `v4.2.1`, users should monitor the `react-component` GitHub organization for official announcements regarding migration paths or potential renaming/superseding of `rc-dropdown` by `@rc-component/dropdown` in future major releases.
- gotcha `rc-dropdown` has strict peer dependency requirements for `react` and `react-dom`, specifically `react@>=16.11.0` and `react-dom@>=16.11.0`. Using older versions of React or ReactDOM will lead to runtime errors, including common "Invalid hook call" issues or unexpected rendering behavior.
Install
-
npm install rc-dropdown -
yarn add rc-dropdown -
pnpm add rc-dropdown
Imports
- Dropdown
const Dropdown = require('rc-dropdown'); import { Dropdown } from 'rc-dropdown';import Dropdown from 'rc-dropdown';
- DropdownProps
import { DropdownProps } from 'rc-dropdown';import type { DropdownProps } from 'rc-dropdown'; - TriggerProps / OverlayProps
import { TriggerProps, OverlayProps } from 'rc-dropdown';import type { TriggerProps, OverlayProps } from 'rc-dropdown';
Quickstart
import React, { useState } from 'react';
import Dropdown from 'rc-dropdown';
import type { DropdownProps } from 'rc-dropdown';
import 'rc-dropdown/assets/index.css'; // Recommended for basic styling structure
const menuOverlay = (
<div style={{
padding: '8px',
background: '#fff',
border: '1px solid #ddd',
borderRadius: '4px',
boxShadow: '0 2px 8px rgba(0,0,0,0.15)'
}}>
<p>Dropdown Item 1</p>
<p>Dropdown Item 2</p>
<p>Dropdown Item 3</p>
</div>
);
const ControlledDropdown: React.FC = () => {
const [visible, setVisible] = useState(false);
const handleVisibleChange: DropdownProps['onVisibleChange'] = (newVisible) => {
console.log('Dropdown visibility changed:', newVisible);
setVisible(newVisible);
};
return (
<div style={{ margin: '50px', display: 'inline-block' }}>
<Dropdown
overlay={menuOverlay}
animation="slide-up"
placement="bottomLeft"
onVisibleChange={handleVisibleChange}
visible={visible}
trigger={['click']} // Also supports 'hover', 'contextMenu'
overlayClassName="my-custom-dropdown-overlay"
>
<button type="button" style={{ padding: '10px 20px', cursor: 'pointer' }}>
{visible ? 'Hide Dropdown' : 'Show Dropdown'}
</button>
</Dropdown>
</div>
);
};
export default ControlledDropdown;