React Native Element Dropdown
React Native Element Dropdown is a versatile library providing highly customizable dropdown and multi-select components for React Native applications. It aims to simplify the creation of selection menus, offering a consistent look and feel across iOS and Android platforms. The current stable version is 2.12.4, with a fairly active release cadence, typically seeing bug fixes and minor feature additions every few months. Key differentiators include built-in support for both single-select dropdowns and multi-select functionality within a single package, extensive customization options for styling (font size, colors, animation duration, 'default', 'modal', or 'auto' display modes), and robust TypeScript support. It also features search capabilities, lazy loading (indicated by keywords), and specific props for controlling item visibility and modal behavior.
Common errors
-
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause Attempting to use `Dropdown` as a default import when it is a named export.fixChange your import statement from `import Dropdown from 'react-native-element-dropdown';` to `import { Dropdown } from 'react-native-element-dropdown';`. -
TypeError: Cannot read property 'label' of undefined (or similar property access errors related to data items).
cause The `labelField` or `valueField` props do not correspond to actual keys present in the objects within your `data` array, or the `data` array itself is empty/invalid.fixVerify that your `data` array contains objects with keys matching exactly what you specify in `labelField` and `valueField`. For example, if `labelField="name"` and `valueField="id"`, your data items should look like `{ name: 'Item Name', id: 'item-id' }`. -
Warning: Failed prop type: Invalid prop `onChange` of type `string` supplied to `Dropdown`, expected `function`.
cause The `onChange` prop was provided with a value that is not a function.fixEnsure that the `onChange` prop receives a valid callback function, for example: `onChange={item => setValue(item.value)}`.
Warnings
- gotcha The `data` prop requires specific `labelField` and `valueField` props to be correctly configured. If these fields do not match the keys in your data array objects, the component will not render items correctly or will throw errors related to undefined properties.
- gotcha Users on older versions (pre-2.10.4) might encounter `scrollToIndex out of range` errors, particularly when dynamically changing the `data` prop to an empty array or manipulating list items in a way that causes the scroll index to become invalid. This has been addressed in recent patches.
- gotcha Versions prior to 2.10.1 had a known bug where list scrolling did not work correctly on Android devices, severely impacting usability for long lists.
- gotcha For applications dealing with very large datasets, the component's default rendering might lead to performance issues if proper virtualization or lazy loading is not implemented. While 'lazy loading' is mentioned in keywords, the core component itself might not inherently virtualize lists without additional configuration or custom render functions.
Install
-
npm install react-native-element-dropdown -
yarn add react-native-element-dropdown -
pnpm add react-native-element-dropdown
Imports
- Dropdown
const Dropdown = require('react-native-element-dropdown');import { Dropdown } from 'react-native-element-dropdown'; - MultiSelect
import MultiSelect from 'react-native-element-dropdown';
import { MultiSelect } from 'react-native-element-dropdown'; - DropdownProps, IDataItem
import { DropdownProps, IDataItem } from 'react-native-element-dropdown';import type { DropdownProps, IDataItem } from 'react-native-element-dropdown';
Quickstart
import React, { useState } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { Dropdown } from 'react-native-element-dropdown';
interface Item { label: string; value: string; }
const data: Item[] = [
{ label: 'Option 1', value: '1' },
{ label: 'Option 2', value: '2' },
{ label: 'Option 3', value: '3' },
{ label: 'Option 4', value: '4' },
{ label: 'Option 5', value: '5' },
];
const DropdownComponent: React.FC = () => {
const [value, setValue] = useState<string | null>(null);
return (
<View style={styles.container}>
<Dropdown
style={styles.dropdown}
placeholderStyle={styles.placeholderStyle}
selectedTextStyle={styles.selectedTextStyle}
inputSearchStyle={styles.inputSearchStyle}
iconStyle={styles.iconStyle}
data={data}
search
maxHeight={300}
labelField="label"
valueField="value"
placeholder="Select item"
searchPlaceholder="Search..."
value={value}
onChange={item => {
setValue(item.value);
}}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: 'white',
padding: 16,
},
dropdown: {
height: 50,
borderColor: 'gray',
borderWidth: 0.5,
borderRadius: 8,
paddingHorizontal: 8,
},
icon: {
marginRight: 5,
},
placeholderStyle: {
fontSize: 16,
},
selectedTextStyle: {
fontSize: 16,
},
iconStyle: {
width: 20,
height: 20,
},
inputSearchStyle: {
height: 40,
fontSize: 16,
},
});
export default DropdownComponent;