picker
Picker is a cross-platform UI component for selecting an item from a list of options.
Top Related Projects
React Native date & time picker component for iOS, Android and Windows
React Native Date Picker is datetime picker for Android and iOS. It includes date, time and datetime picker modes. The datepicker is customizable and is supporting different languages. It's written with native code to achieve the best possible look, feel and performance.
Material Design for React Native (Android & iOS)
Cross-Platform React Native UI Toolkit
UI Components Library for React Native
Quick Overview
React Native Picker is a cross-platform picker component for React Native applications. It provides a native UI component for selecting items from a list, with support for both iOS and Android platforms. The library aims to offer a seamless and consistent user experience across different devices.
Pros
- Cross-platform compatibility (iOS and Android)
- Native UI components for better performance and user experience
- Customizable appearance and behavior
- Active maintenance and community support
Cons
- Limited styling options compared to custom-built pickers
- Occasional platform-specific issues or inconsistencies
- Learning curve for developers new to React Native
- Dependency on React Native, which may not be suitable for all projects
Code Examples
- Basic usage of the Picker component:
import {Picker} from '@react-native-picker/picker';
<Picker
selectedValue={selectedLanguage}
onValueChange={(itemValue, itemIndex) =>
setSelectedLanguage(itemValue)
}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
- Using Picker with state in a functional component:
import React, {useState} from 'react';
import {Picker} from '@react-native-picker/picker';
const LanguagePicker = () => {
const [selectedLanguage, setSelectedLanguage] = useState();
return (
<Picker
selectedValue={selectedLanguage}
onValueChange={(itemValue, itemIndex) =>
setSelectedLanguage(itemValue)
}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
<Picker.Item label="Python" value="python" />
</Picker>
);
};
- Styling the Picker component:
import {Picker} from '@react-native-picker/picker';
<Picker
selectedValue={selectedValue}
onValueChange={(itemValue, itemIndex) => setSelectedValue(itemValue)}
style={{height: 50, width: 150}}
itemStyle={{fontSize: 16, color: 'blue'}}>
<Picker.Item label="Option 1" value="option1" />
<Picker.Item label="Option 2" value="option2" />
</Picker>
Getting Started
-
Install the package:
npm install @react-native-picker/picker
-
For iOS, run:
cd ios && pod install
-
Import and use the Picker component in your React Native app:
import {Picker} from '@react-native-picker/picker'; function MyComponent() { return ( <Picker selectedValue={selectedValue} onValueChange={(itemValue, itemIndex) => setSelectedValue(itemValue)}> <Picker.Item label="Option 1" value="option1" /> <Picker.Item label="Option 2" value="option2" /> </Picker> ); }
Competitor Comparisons
React Native date & time picker component for iOS, Android and Windows
Pros of datetimepicker
- Specialized for date and time selection, offering a more focused and optimized user experience
- Native look and feel on both iOS and Android platforms
- Supports various modes: date, time, and datetime
Cons of datetimepicker
- Limited to date and time selection, less versatile than picker for general-purpose use
- May require additional configuration for custom styling or advanced features
Code Comparison
datetimepicker:
import DateTimePicker from '@react-native-community/datetimepicker';
<DateTimePicker
value={date}
mode="date"
display="default"
onChange={onChange}
/>
picker:
import {Picker} from '@react-native-picker/picker';
<Picker
selectedValue={selectedValue}
onValueChange={(itemValue, itemIndex) => setSelectedValue(itemValue)}
>
<Picker.Item label="Option 1" value="option1" />
<Picker.Item label="Option 2" value="option2" />
</Picker>
The datetimepicker is specifically designed for date and time selection, providing a native interface on both platforms. It's ideal for applications that require precise date and time input. However, it's limited to this specific use case.
The picker, on the other hand, is more versatile and can be used for various types of selection, including custom options. It's more flexible but may require additional styling to achieve a native look and feel for date and time selection.
Choose datetimepicker for dedicated date/time functionality, and picker for more general-purpose selection needs in your React Native application.
React Native Date Picker is datetime picker for Android and iOS. It includes date, time and datetime picker modes. The datepicker is customizable and is supporting different languages. It's written with native code to achieve the best possible look, feel and performance.
Pros of react-native-date-picker
- Specialized for date and time picking, offering more date-specific features
- Supports both iOS and Android with a consistent look and feel
- Provides more customization options for date and time formats
Cons of react-native-date-picker
- Limited to date and time selection, less versatile for other types of data
- May have a steeper learning curve due to more date-specific props and options
- Potentially larger package size due to specialized date handling functionality
Code Comparison
react-native-picker/picker:
<Picker
selectedValue={selectedValue}
onValueChange={(itemValue, itemIndex) => setSelectedValue(itemValue)}
>
<Picker.Item label="Option 1" value="option1" />
<Picker.Item label="Option 2" value="option2" />
</Picker>
react-native-date-picker:
<DatePicker
date={date}
onDateChange={setDate}
mode="date"
minimumDate={new Date()}
maximumDate={new Date(2023, 12, 31)}
/>
Summary
react-native-picker/picker is a general-purpose picker component suitable for various data types, while react-native-date-picker is specialized for date and time selection. The latter offers more date-specific features and customization but is limited to date/time data. The general picker is more versatile but may require additional work for complex date handling. Choose based on your specific requirements and the complexity of your date/time selection needs.
Material Design for React Native (Android & iOS)
Pros of react-native-paper
- Comprehensive UI component library with a wide range of pre-built components
- Material Design implementation for consistent and modern look
- Active development and community support
Cons of react-native-paper
- Larger bundle size due to the extensive component library
- May require additional configuration for custom styling
- Learning curve for developers new to Material Design principles
Code Comparison
react-native-picker/picker:
import {Picker} from '@react-native-picker/picker';
<Picker
selectedValue={selectedLanguage}
onValueChange={(itemValue, itemIndex) =>
setSelectedLanguage(itemValue)
}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
react-native-paper:
import {Picker} from 'react-native-paper';
<Picker
selectedValue={selectedLanguage}
onValueChange={(itemValue) => setSelectedLanguage(itemValue)}
mode="dropdown">
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
Summary
react-native-picker/picker is a focused solution for picker components, while react-native-paper offers a comprehensive UI library. The choice depends on project requirements, with react-native-paper providing more components but potentially increasing bundle size and complexity. The code usage is similar, with react-native-paper offering additional styling options and Material Design integration.
Cross-Platform React Native UI Toolkit
Pros of react-native-elements
- Comprehensive UI toolkit with a wide range of pre-built components
- Consistent design language across all components
- Highly customizable with theming support
Cons of react-native-elements
- Larger bundle size due to the extensive component library
- May require more setup and configuration for individual components
- Potential performance impact when using many complex components
Code Comparison
react-native-elements:
import { Button, Input, Icon } from 'react-native-elements';
<Button
title="Submit"
icon={<Icon name="check" color="white" />}
buttonStyle={{ backgroundColor: 'blue' }}
/>
<Input
placeholder="Enter your name"
leftIcon={{ type: 'font-awesome', name: 'user' }}
/>
react-native-picker/picker:
import { Picker } from '@react-native-picker/picker';
<Picker
selectedValue={selectedValue}
onValueChange={(itemValue, itemIndex) => setSelectedValue(itemValue)}
>
<Picker.Item label="Option 1" value="option1" />
<Picker.Item label="Option 2" value="option2" />
</Picker>
The react-native-elements library offers a more extensive set of UI components with consistent styling and customization options. However, it may come with a larger bundle size and potential performance considerations. The react-native-picker/picker library focuses specifically on providing a picker component, which may be more lightweight and easier to integrate if that's the only functionality needed.
UI Components Library for React Native
Pros of react-native-ui-lib
- Comprehensive UI component library with a wide range of pre-built components
- Consistent design system and theming capabilities
- Includes additional utilities and helpers for React Native development
Cons of react-native-ui-lib
- Larger package size due to the extensive component library
- Steeper learning curve for developers unfamiliar with the library's conventions
- May require more setup and configuration compared to simpler picker solutions
Code Comparison
react-native-picker/picker:
import {Picker} from '@react-native-picker/picker';
<Picker
selectedValue={selectedLanguage}
onValueChange={(itemValue, itemIndex) => setSelectedLanguage(itemValue)}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
react-native-ui-lib:
import {Picker} from 'react-native-ui-lib';
<Picker
value={selectedLanguage}
onChange={value => setSelectedLanguage(value)}
placeholder="Select a language"
floatingPlaceholder
enableModalBlur={false}
topBarProps={{title: 'Languages'}}>
<Picker.Item key="java" value="java" label="Java" />
<Picker.Item key="js" value="js" label="JavaScript" />
</Picker>
The react-native-ui-lib Picker offers more customization options and styling features out of the box, while the react-native-picker/picker provides a simpler, more lightweight implementation focused specifically on the picker functionality.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
@react-native-picker/picker
Android | iOS | PickerIOS | Windows | MacOS |
---|---|---|---|---|
Supported Versions
@react-native-picker/picker | react-native | react-native-windows |
---|---|---|
>= 2.0.0 | 0.61+ | 0.64+ |
>= 1.16.0 | 0.61+ | 0.61+ |
>= 1.2.0 | 0.60+ or 0.59+ with Jetifier | N/A |
>= 1.0.0 | 0.57 | N/A |
Getting started
$ npm install @react-native-picker/picker --save
or
$ yarn add @react-native-picker/picker
For React Native v0.60 and above (Autolinking)
As react-native@0.60 and above supports autolinking there is no need to run the linking process.
Read more about autolinking here. This is supported by react-native-windows@0.64
and above.
iOS
CocoaPods on iOS needs this extra step:
npx pod-install
Android
No additional step is required.
Windows (expand for details)
Windows
Usage in Windows without autolinking requires the following extra steps:
Add the ReactNativePicker
project to your solution.
- Open the solution in Visual Studio 2019
- Right-click Solution icon in Solution Explorer > Add > Existing Project
Select
D:\dev\RNTest\node_modules\@react-native-picker\picker\windows\ReactNativePicker\ReactNativePicker.vcxproj
windows/myapp.sln
Add a reference to ReactNativePicker
to your main application project. From Visual Studio 2019:
Right-click main application project > Add > Reference...
Check ReactNativePicker
from Solution Projects.
app.cpp
Add #include "winrt/ReactNativePicker.h"
to the headers included at the top of the file.
Add PackageProviders().Append(winrt::ReactNativePicker::ReactPackageProvider());
before InitializeComponent();
.
MacOS
CocoaPods on MacOS needs this extra step (called from the MacOS directory)
pod install
React Native below 0.60 (Link and Manual Installation)
The following steps are only necessary if you are working with a version of React Native lower than 0.60
Mostly automatic installation
$ react-native link @react-native-picker/picker
Manual installation
iOS
- In XCode, in the project navigator, right click
Libraries
âAdd Files to [your project's name]
- Go to
node_modules
â@react-native-picker/picker
and addRNCPicker.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNCPicker.a
to your project'sBuild Phases
âLink Binary With Libraries
- Run your project (
Cmd+R
)<
Android
- Open application file (
android/app/src/main/java/[...]/MainApplication.java
)
- Add
import com.reactnativecommunity.picker.RNCPickerPackage;
to the imports at the top of the file - Add
new RNCPickerPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':@react-native-picker_picker' project(':@react-native-picker_picker').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-picker/picker/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:implementation project(path: ':@react-native-picker_picker')
MacOS
- In XCode, in the project navigator, right click
Libraries
âAdd Files to [your project's name]
- Go to
node_modules
â@react-native-picker/picker
and addRNCPicker.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNCPicker.a
to your project'sBuild Phases
âLink Binary With Libraries
- Run your project (
Cmd+R
)<
RTL Support
you need to add android:supportsRtl="true"
to AndroidManifest.xml
<application
...
android:supportsRtl="true">
Usage
Import Picker from @react-native-picker/picker
:
import {Picker} from '@react-native-picker/picker';
Create state which will be used by the Picker
:
const [selectedLanguage, setSelectedLanguage] = useState();
Add Picker
like this:
<Picker
selectedValue={selectedLanguage}
onValueChange={(itemValue, itemIndex) =>
setSelectedLanguage(itemValue)
}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
If you want to open/close picker programmatically on android (available from version 1.16.0+), pass ref to Picker
:
const pickerRef = useRef();
function open() {
pickerRef.current.focus();
}
function close() {
pickerRef.current.blur();
}
return <Picker
ref={pickerRef}
selectedValue={selectedLanguage}
onValueChange={(itemValue, itemIndex) =>
setSelectedLanguage(itemValue)
}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
Props
Reference
Props
onValueChange
Callback for when an item is selected. This is called with the following parameters:
itemValue
: thevalue
prop of the item that was selecteditemPosition
: the index of the selected item in this picker
Type | Required |
---|---|
function | No |
selectedValue
Value matching value of one of the items. Can be a string or an integer.
Type | Required |
---|---|
any | No |
style
Type | Required |
---|---|
pickerStyleType | No |
testID
Used to locate this view in end-to-end tests.
Type | Required |
---|---|
string | No |
enabled
If set to false, the picker will be disabled, i.e. the user will not be able to make a selection.
Type | Required | Platform |
---|---|---|
bool | No | Android, Web, Windows |
mode
On Android, specifies how to display the selection items when the user taps on the picker:
- 'dialog': Show a modal dialog. This is the default.
- 'dropdown': Shows a dropdown anchored to the picker view
Type | Required | Platform |
---|---|---|
enum('dialog', 'dropdown') | No | Android |
dropdownIconColor
On Android, specifies color of dropdown triangle. Input value should be value that is accepted by react-native processColor
function.
Type | Required | Platform |
---|---|---|
ColorValue | No | Android |
dropdownIconRippleColor
On Android, specifies ripple color of dropdown triangle. Input value should be value that is accepted by react-native processColor
function.
Type | Required | Platform |
---|---|---|
ColorValue | No | Android |
prompt
Prompt string for this picker, used on Android in dialog mode as the title of the dialog.
Type | Required | Platform |
---|---|---|
string | No | Android |
itemStyle
Style to apply to each of the item labels.
Type | Required | Platform |
---|---|---|
text styles | No | iOS, Windows |
numberOfLines
On Android & iOS, used to truncate the text with an ellipsis after computing the text layout, including line wrapping, such that the total number of lines does not exceed this number. Default is '1'
Type | Required | Platform |
---|---|---|
number | No | Android, iOS |
onBlur
Type | Required | Platform |
---|---|---|
function | No | Android |
onFocus
Type | Required | Platform |
---|---|---|
function | No | Android |
selectionColor
Type | Required | Platform |
---|---|---|
ColorValue | No | iOS |
Methods
blur
(Android only, lib version 1.16.0+)
Programmatically closes picker
focus
(Android only, lib version 1.16.0+)
Programmatically opens picker
PickerItemProps
Props that can be applied to individual Picker.Item
label
Displayed value on the Picker Item
Type | Required |
---|---|
string | Yes |
value
Actual value on the Picker Item
Type | Required |
---|---|
number,string | Yes |
color
Displayed color on the Picker Item
Type | Required |
---|---|
ColorValue | No |
fontFamily
Displayed fontFamily on the Picker Item
Type | Required |
---|---|
string | No |
style
Style to apply to individual item labels.
Type | Required | Platform |
---|---|---|
ViewStyleProp | No | Android |
enabled
If set to false, the specific item will be disabled, i.e. the user will not be able to make a selection
@default: true
Type | Required | Platform |
---|---|---|
boolean | No | Android |
contentDescription
Sets the content description to the Picker Item
Type | Required | Platform |
---|---|---|
string | No | Android |
PickerIOS
Props
Reference
Props
itemStyle
Type | Required |
---|---|
text styles | No |
onValueChange
Type | Required |
---|---|
function | No |
selectedValue
Type | Required |
---|---|
any | No |
selectionColor
Type | Required | Platform |
---|---|---|
ColorValue | No | iOS |
themeVariant
Type | Required |
---|---|
enum('light', 'dark') | No |
Top Related Projects
React Native date & time picker component for iOS, Android and Windows
React Native Date Picker is datetime picker for Android and iOS. It includes date, time and datetime picker modes. The datepicker is customizable and is supporting different languages. It's written with native code to achieve the best possible look, feel and performance.
Material Design for React Native (Android & iOS)
Cross-Platform React Native UI Toolkit
UI Components Library for React Native
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot