To use react-native-dropdown-picker, you must manage the component's state (open status, selected value, and the items list) using React state hooks. The component requires several props to control its behavior and update its state:
open: Boolean indicating if the dropdown is open.value: The currently selected item's value.items: An array of objects, where each object has a label and a value.setOpen: State setter function for the open state.setValue: State setter function for the value state.setItems: State setter function for the items state.placeholder: String displayed when no value is selected.
import React, {useState} from 'react';
import {View, Text} from 'react-native';
import DropDownPicker from 'react-native-dropdown-picker';
export default function App() {
const [open, setOpen] = useState(false);
const [value, setValue] = useState(null);
const [items, setItems] = useState([
{label: 'Apple', value: 'apple'},
{label: 'Banana', value: 'banana'},
{label: 'Pear', value: 'pear'},
]);
return (
<View style={{flex: 1}}>
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: 15,
}}>
<DropDownPicker
open={open}
value={value}
items={items}
setOpen={setOpen}
setValue={setValue}
setItems={setItems}
placeholder={'Choose a fruit.'}
/>
</View>
<View style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center'
}}>
<Text>Chosen fruit: {value === null ? 'none' : value}</Text>
</View>
</View>
);
}