------------------------- | Select a fruit: | | [Picker ▼] | | | | Select a date: | | [Date Picker Button] | | | | Selected fruit: | | [fruit name] | | Selected date: | | [date in YYYY-MM-DD] | -------------------------
Picker and date picker in React Native - Mini App: Build & Ship
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 PickerDatePickerScreen() { const [selectedFruit, setSelectedFruit] = useState('Apple'); const [date, setDate] = useState(new Date()); const [showDatePicker, setShowDatePicker] = useState(false); // TODO: Add picker and date picker UI here return ( <View style={{ padding: 20 }}> {/* TODO: Add Picker component here */} {/* TODO: Add button to open date picker here */} {/* TODO: Show selected fruit and date here */} </View> ); }
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 PickerDatePickerScreen() { const [selectedFruit, setSelectedFruit] = useState('Apple'); const [date, setDate] = useState(new Date()); const [showDatePicker, setShowDatePicker] = useState(false); const onChange = (event, selectedDate) => { setShowDatePicker(Platform.OS === 'ios'); if (selectedDate) { setDate(selectedDate); } }; const formattedDate = date.toISOString().split('T')[0]; return ( <View style={{ padding: 20 }}> <Text style={{ marginBottom: 8, fontWeight: 'bold' }}>Select a fruit:</Text> <Picker selectedValue={selectedFruit} onValueChange={(itemValue) => setSelectedFruit(itemValue)} style={{ marginBottom: 20 }} accessibilityLabel="Fruit picker" > <Picker.Item label="Apple" value="Apple" /> <Picker.Item label="Banana" value="Banana" /> <Picker.Item label="Cherry" value="Cherry" /> </Picker> <Text style={{ marginBottom: 8, fontWeight: 'bold' }}>Select a date:</Text> <Button title="Pick a date" onPress={() => setShowDatePicker(true)} accessibilityLabel="Open date picker" /> {showDatePicker && ( <DateTimePicker value={date} mode="date" display="default" onChange={onChange} maximumDate={new Date(2100, 12, 31)} minimumDate={new Date(1900, 0, 1)} accessibilityLabel="Date picker" /> )} <View style={{ marginTop: 30 }}> <Text style={{ fontWeight: 'bold' }}>Selected fruit:</Text> <Text>{selectedFruit}</Text> <Text style={{ fontWeight: 'bold', marginTop: 20 }}>Selected date:</Text> <Text>{formattedDate}</Text> </View> </View> ); }
This solution uses React Native's Picker from '@react-native-picker/picker' to let the user select a fruit. The selected fruit is stored in state and updated on change.
For the date picker, it uses '@react-native-community/datetimepicker'. A button toggles the visibility of the date picker modal. When the user picks a date, the state updates and the picker closes (except on iOS where it stays open).
The selected fruit and date are displayed below the controls. The date is formatted as YYYY-MM-DD using toISOString().split('T')[0].
Accessibility labels are added for screen readers. The UI uses simple Text and Button components with spacing for clarity.
------------------------- | Select a fruit: | | [Apple ▼] | | | | Select a date: | | [Pick a date] | | | | Selected fruit: | | Apple | | Selected date: | | 2024-06-15 | -------------------------
Practice
Picker component in React Native?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]
- Confusing Picker with text input
- Thinking Picker plays media
- Assuming Picker shows images
Picker component from @react-native-picker/picker?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]
- Using default import instead of named import
- Importing from wrong package
- Missing curly braces around 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>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]
- Assuming state does not update on selection
- Confusing label and value
- Expecting default value always
const [date, setDate] = useState(new Date());
<DatePicker
value={date}
onChange={(event, selectedDate) => setDate(selectedDate)}
/>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]
- Omitting 'mode' prop causes no date shown
- Confusing 'value' with 'selected' prop
- Ignoring event parameter in onChange
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]
- Using one state object but not updating it
- Delaying state update until submit
- Using refs instead of state for UI updates
Ready to prove it?
Pass the test and this pattern moves unknown
