Challenge - 5 Problems
Picker and DatePicker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate1:30remaining
Picker selection updates state correctly
What will be the value of
selectedFruit after selecting "Banana" from the Picker?React Native
import React, { useState } from 'react'; import { View, Text } from 'react-native'; import { Picker } from '@react-native-picker/picker'; export default function FruitPicker() { const [selectedFruit, setSelectedFruit] = useState('Apple'); return ( <View> <Picker selectedValue={selectedFruit} onValueChange={(itemValue) => setSelectedFruit(itemValue)} > <Picker.Item label="Apple" value="Apple" /> <Picker.Item label="Banana" value="Banana" /> <Picker.Item label="Cherry" value="Cherry" /> </Picker> <Text>Selected: {selectedFruit}</Text> </View> ); }
Attempts:
2 left
💡 Hint
The
onValueChange updates the state with the selected value.✗ Incorrect
When the user selects "Banana", the
onValueChange callback sets selectedFruit to "Banana". The Text component then shows "Selected: Banana".❓ ui_behavior
intermediate2:00remaining
DatePicker initial date and change behavior
Given the code below, what will be displayed in the Text component after the user picks March 15, 2024?
React Native
import React, { useState } from 'react'; import { View, Text, Button, Platform } from 'react-native'; import DateTimePicker from '@react-native-community/datetimepicker'; export default function DatePickerExample() { const [date, setDate] = useState(new Date(2023, 0, 1)); const [show, setShow] = useState(false); const onChange = (event, selectedDate) => { setShow(Platform.OS === 'ios'); if (selectedDate) { setDate(selectedDate); } }; return ( <View> <Button title="Pick a date" onPress={() => setShow(true)} /> {show && ( <DateTimePicker value={date} mode="date" display="default" onChange={onChange} /> )} <Text>Selected date: {date.toDateString()}</Text> </View> ); }
Attempts:
2 left
💡 Hint
The
onChange updates the date state with the picked date.✗ Incorrect
When the user picks March 15, 2024, the
onChange sets date to that date. The Text component shows the date in a readable string format.❓ lifecycle
advanced1:30remaining
DatePicker visibility on Android vs iOS
Why does the DateTimePicker disappear immediately after selecting a date on Android but not on iOS in the code below?
React Native
const onChange = (event, selectedDate) => {
setShow(Platform.OS === 'ios');
if (selectedDate) {
setDate(selectedDate);
}
};Attempts:
2 left
💡 Hint
Check how
setShow is set depending on platform.✗ Incorrect
The code sets
show to false on Android after selection, hiding the picker. On iOS, it remains true, so the picker stays visible.📝 Syntax
advanced1:00remaining
Correct syntax for Picker.Item inside Picker
Which option shows the correct syntax to add a Picker.Item with label "Orange" and value "orange" inside a Picker?
Attempts:
2 left
💡 Hint
Remember JSX syntax requires quotes around string props.
✗ Incorrect
Option C uses correct JSX syntax with double quotes around string props and self-closing tag.
🔧 Debug
expert2:30remaining
Why does the DatePicker not update the displayed date?
Given the code below, why does the displayed date never change after picking a new date?
React Native
import React, { useState } from 'react'; import { View, Text, Button } from 'react-native'; import DateTimePicker from '@react-native-community/datetimepicker'; export default function BrokenDatePicker() { const [date, setDate] = useState(new Date()); const [show, setShow] = useState(false); const onChange = (event, selectedDate) => { setShow(false); if (selectedDate) { date = selectedDate; } }; return ( <View> <Button title="Pick date" onPress={() => setShow(true)} /> {show && <DateTimePicker value={date} mode="date" onChange={onChange} />} <Text>Selected date: {date.toDateString()}</Text> </View> ); }
Attempts:
2 left
💡 Hint
In React, state must be updated with the setter function, not by direct assignment.
✗ Incorrect
The line
date = selectedDate; only changes the local variable, not the React state. The component does not re-render with the new date.