0
0
React Nativemobile~20 mins

Picker and date picker in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Picker and DatePicker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
1: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>
  );
}
A"Banana"
B"Apple"
C"Cherry"
Dundefined
Attempts:
2 left
💡 Hint
The onValueChange updates the state with the selected value.
ui_behavior
intermediate
2: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>
  );
}
ASelected date: Thu Mar 14 2024
BSelected date: Fri Mar 15 2024
CSelected date: Sun Jan 01 2023
DSelected date: undefined
Attempts:
2 left
💡 Hint
The onChange updates the date state with the picked date.
lifecycle
advanced
1: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);
  }
};
ABecause on iOS, <code>setShow(false)</code> is called immediately after selection.
BBecause on iOS, the picker does not call <code>onChange</code> after selection.
CBecause on Android, the picker is not rendered at all.
DBecause on Android, <code>setShow(false)</code> hides the picker after selection; on iOS, it stays visible.
Attempts:
2 left
💡 Hint
Check how setShow is set depending on platform.
📝 Syntax
advanced
1: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?
A<Picker.Item label='Orange' value=orange />
B<Picker.Item label=Orange value="orange" />
C<Picker.Item label="Orange" value="orange" />
D<Picker.Item label="Orange" value="orange"> </Picker.Item>
Attempts:
2 left
💡 Hint
Remember JSX syntax requires quotes around string props.
🔧 Debug
expert
2: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>
  );
}
ABecause the code assigns to the variable 'date' directly instead of using setDate to update state.
BBecause the DateTimePicker component is missing the 'display' prop.
CBecause the initial state date is not set to a valid Date object.
DBecause the onChange callback does not call setShow(true).
Attempts:
2 left
💡 Hint
In React, state must be updated with the setter function, not by direct assignment.