React Dropdown Component
React Dropdown is a simple, lightweight component designed for creating custom-styled dropdowns in React applications, currently at version 1.11.0. It addresses the styling limitations of native HTML `<select>` elements and supports grouped options, which are not easily achievable with standard HTML. Unlike more feature-rich libraries like `react-select`, this package prioritizes simplicity and customizability for basic dropdown needs, offering granular control over CSS classes for various parts of the component. While it has been stable, development appears to be inactive, with the last publish dating back over four years. It ships with TypeScript types and supports a wide range of React versions via peer dependencies, from 0.14.7 up to 18.0.0.
Common errors
-
Module not found: Can't resolve 'react-dropdown/style.css'
cause The CSS stylesheet for the component's default styling was not imported.fixAdd `import 'react-dropdown/style.css';` to your application's entry point or the component where `Dropdown` is used. -
TypeError: Dropdown is not a function or is not iterable
cause Incorrect import statement, particularly when mixing CommonJS `require` with default ESM exports or using named imports for a default export.fixFor ESM, use `import Dropdown from 'react-dropdown';`. For CommonJS, use `const Dropdown = require('react-dropdown').default;`. -
TypeError: Cannot read properties of undefined (reading 'value') in onChange handler
cause The `onChange` handler is attempting to access `option.value` when `option` itself is `undefined` or not an object, or `this` context is lost in a class component.fixVerify that the `onChange` handler receives a valid `option` object. If in a class component, ensure the handler is correctly bound to `this` (e.g., `onChange={this._onSelect.bind(this)}` or use an arrow function property). -
Dropdown appears but clicking it does nothing or the menu doesn't open.
cause The `disabled` prop might be inadvertently set to `true`, preventing user interaction.fixCheck if the `disabled` prop is present on the `Dropdown` component. Remove it or ensure its value evaluates to `false` if the dropdown should be interactive.
Warnings
- breaking For React versions older than `v0.13`, you must use `react-dropdown@v0.6.1`. Using `v1.x` or later with legacy React versions will result in runtime errors due to API changes.
- gotcha The component's default styling is not automatically applied. Forgetting to import `'react-dropdown/style.css'` will result in an unstyled and potentially visually broken dropdown.
- gotcha This library offers a simple dropdown component and explicitly notes it is not `react-select`. It does not provide advanced features like multi-select, search filtering, asynchronous options loading, or virtualization found in more complex libraries.
- gotcha The `react-dropdown` package appears to be unmaintained since its last release over four years ago. This may lead to compatibility issues with newer React versions or ecosystem changes, potential security vulnerabilities not addressed, or difficulties in getting community support.
- gotcha When using the `onChange` handler within a React class component, issues with `this` context can occur if the method is not properly bound.
Install
-
npm install react-dropdown -
yarn add react-dropdown -
pnpm add react-dropdown
Imports
- Dropdown
import { Dropdown } from 'react-dropdown';import Dropdown from 'react-dropdown';
- Dropdown CSS
import Dropdown from 'react-dropdown/style.css';
import 'react-dropdown/style.css';
- Option type
import type { Option } from 'react-dropdown';
Quickstart
import React, { useState } from 'react';
import Dropdown from 'react-dropdown';
import 'react-dropdown/style.css';
const options = [
'one', 'two', 'three',
{ type: 'group', name: 'Group 1', items: [
{ value: 'four', label: 'Four' },
{ value: 'five', label: 'Five' }
]}
];
function MyDropdownComponent() {
const [selectedOption, setSelectedOption] = useState(options[0]);
const _onSelect = (option) => {
setSelectedOption(option);
console.log('Selected:', option.value);
};
return (
<div className="dropdown-container">
<h2>Basic Dropdown Example</h2>
<Dropdown
options={options}
onChange={_onSelect}
value={selectedOption}
placeholder="Select an option"
className="my-custom-dropdown"
controlClassName="my-custom-control"
menuClassName="my-custom-menu"
/>
<p>Current selection: {selectedOption ? selectedOption.label || selectedOption : 'None'}</p>
</div>
);
}
export default MyDropdownComponent;