Pickers let users choose one option from a list easily. Date pickers help users select a date without typing.
Picker and date picker in React Native
Introduction
Syntax
React Native
import { Picker } from '@react-native-picker/picker'; import DateTimePicker from '@react-native-community/datetimepicker'; // Picker example <Picker selectedValue={selectedValue} onValueChange={(itemValue) => setSelectedValue(itemValue)}> <Picker.Item label="Option 1" value="1" /> <Picker.Item label="Option 2" value="2" /> </Picker> // DateTimePicker example <DateTimePicker value={date} mode="date" display="default" onChange={onChange} />
Picker needs a selectedValue and onValueChange to work.
DateTimePicker needs a date value and an onChange handler.
Examples
React Native
import { Picker } from '@react-native-picker/picker'; <Picker selectedValue={fruit} onValueChange={setFruit}> <Picker.Item label="Apple" value="apple" /> <Picker.Item label="Banana" value="banana" /> </Picker>
React Native
import DateTimePicker from '@react-native-community/datetimepicker'; <DateTimePicker value={new Date()} mode="date" display="spinner" onChange={(event, selectedDate) => setDate(selectedDate)} />
Sample App
This app shows a fruit picker and a button to open a date picker. The selected fruit and date appear below.
React Native
import React, { useState } from 'react'; import { View, Text, Button, Platform } from 'react-native'; import { Picker } from '@react-native-picker/picker'; import DateTimePicker from '@react-native-community/datetimepicker'; export default function App() { const [fruit, setFruit] = useState('apple'); const [date, setDate] = useState(new Date()); const [show, setShow] = useState(false); const onChange = (event, selectedDate) => { const currentDate = selectedDate || date; setShow(Platform.OS === 'ios'); setDate(currentDate); }; return ( <View style={{ flex: 1, justifyContent: 'center', padding: 20 }}> <Text style={{ fontSize: 18, marginBottom: 10 }}>Pick a fruit:</Text> <Picker selectedValue={fruit} onValueChange={(itemValue) => setFruit(itemValue)} accessibilityLabel="Fruit picker" > <Picker.Item label="Apple" value="apple" /> <Picker.Item label="Banana" value="banana" /> <Picker.Item label="Orange" value="orange" /> </Picker> <Text style={{ fontSize: 18, marginVertical: 10 }}>Selected fruit: {fruit}</Text> <Button onPress={() => setShow(true)} title="Pick a date" accessibilityLabel="Show date picker" /> {show && ( <DateTimePicker value={date} mode="date" display="default" onChange={onChange} accessibilityLabel="Date picker" /> )} <Text style={{ fontSize: 18, marginTop: 10 }}> Selected date: {date.toDateString()} </Text> </View> ); }
Important Notes
Picker works well for short lists. For long lists, consider searchable dropdowns.
DateTimePicker behaves differently on iOS and Android; test on both.
Always provide accessibility labels for better user experience.
Summary
Pickers let users select one option from a list easily.
Date pickers help users choose dates without typing.
Use state to track selected values and update UI accordingly.
Practice
1. What is the main purpose of a
Picker component in React Native?easy
Solution
Step 1: Understand Picker usage
A Picker is designed to show a list of options for the user to choose from.Step 2: Compare with other components
Other options like text input or image gallery serve different purposes, not selection from a list.Final Answer:
To let users select one option from a list -> Option DQuick Check:
Picker = select one option [OK]
Hint: Picker is for choosing one item from many [OK]
Common Mistakes:
- Confusing Picker with text input
- Thinking Picker plays media
- Assuming Picker shows images
2. Which of the following is the correct way to import the
Picker component from @react-native-picker/picker?easy
Solution
Step 1: Check official import syntax
The Picker is exported as a named export from '@react-native-picker/picker', so curly braces are needed.Step 2: Eliminate incorrect imports
Default import from the correct package or imports from wrong packages are incorrect.Final Answer:
import { Picker } from '@react-native-picker/picker'; -> Option CQuick Check:
Named import with braces [OK]
Hint: Use curly braces for named imports like Picker [OK]
Common Mistakes:
- Using default import instead of named import
- Importing from wrong package
- Missing curly braces around Picker
3. Given this code snippet, what will be displayed when the user selects "Banana" from the Picker?
const [fruit, setFruit] = useState('Apple');
<Picker selectedValue={fruit} onValueChange={setFruit}>
<Picker.Item label="Apple" value="Apple" />
<Picker.Item label="Banana" value="Banana" />
<Picker.Item label="Cherry" value="Cherry" />
</Picker>
<Text>Selected fruit: {fruit}</Text>medium
Solution
Step 1: Understand state update on selection
The onValueChange calls setFruit with the selected value, updating the fruit state.Step 2: Check selected value after choosing Banana
When Banana is selected, fruit becomes 'Banana', so the Text shows 'Selected fruit: Banana'.Final Answer:
Selected fruit: Banana -> Option AQuick Check:
State updates to selected value = Banana [OK]
Hint: Selected value updates state shown in Text [OK]
Common Mistakes:
- Assuming state does not update on selection
- Confusing label and value
- Expecting default value always
4. Identify the error in this DatePicker usage:
const [date, setDate] = useState(new Date());
<DatePicker
value={date}
onChange={(event, selectedDate) => setDate(selectedDate)}
/>medium
Solution
Step 1: Review DatePicker required props
React Native DatePicker requires a 'mode' prop like 'date' or 'time' to work properly.Step 2: Check other props correctness
'value' and 'onChange' usage is correct; 'setDate' is correctly called to update state.Final Answer:
The DatePicker component requires 'mode' prop -> Option BQuick Check:
Missing required 'mode' prop [OK]
Hint: DatePicker needs 'mode' prop to work [OK]
Common Mistakes:
- Omitting 'mode' prop causes no date shown
- Confusing 'value' with 'selected' prop
- Ignoring event parameter in onChange
5. You want to create a form with a Picker for selecting a country and a DatePicker for selecting a birthdate. How do you ensure the UI updates correctly when the user changes either input?
hard
Solution
Step 1: Manage state for each input separately
Use one state variable for country and another for birthdate to track changes independently.Step 2: Update state on user interaction
Update the corresponding state in onValueChange for Picker and onChange for DatePicker to reflect UI changes.Final Answer:
Use separate state variables for country and birthdate, update each on change -> Option AQuick Check:
Separate states updated on change [OK]
Hint: Track each input with its own state variable [OK]
Common Mistakes:
- Using one state object but not updating it
- Delaying state update until submit
- Using refs instead of state for UI updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
