Bird
Raised Fist0
React Nativemobile~10 mins

Picker and date picker in React Native - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Picker component from @react-native-picker/picker.

React Native
import { [1] } from '@react-native-picker/picker';
Drag options to blanks, or click blank then click option'
ASelect
BDatePicker
CPicker
DDropdown
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'DatePicker' instead of 'Picker'.
Trying to import 'Select' which is not from this package.
2fill in blank
medium

Complete the code to set the selected value state for the Picker component.

React Native
const [selectedValue, setSelectedValue] = useState([1]);
Drag options to blanks, or click blank then click option'
A"java"
Bnull
C0
D""
Attempts:
3 left
💡 Hint
Common Mistakes
Setting initial value to null or empty string which does not match any Picker item.
Using a number instead of a string.
3fill in blank
hard

Fix the error in the Picker onValueChange handler to update the selected value.

React Native
<Picker
  selectedValue={selectedValue}
  onValueChange={(itemValue, itemIndex) => [1]
>
Drag options to blanks, or click blank then click option'
AsetSelectedValue(itemIndex)
BsetSelectedValue(itemValue)
CselectedValue = itemValue
DsetSelectedValue()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing itemIndex instead of itemValue.
Trying to assign directly to selectedValue which is read-only.
4fill in blank
hard

Fill both blanks to create a DateTimePicker that shows when showPicker is true and updates the date state.

React Native
const [date, setDate] = useState(new Date());
const [showPicker, setShowPicker] = useState(false);

<DateTimePicker
  value={date}
  mode=[1]
  display="default"
  onChange={(event, selectedDate) => {
    setShowPicker(false);
    if (selectedDate) {
      [2];
    }
  }}
/>
Drag options to blanks, or click blank then click option'
A"date"
BsetDate(selectedDate)
CsetDate(event)
D"time"
Attempts:
3 left
💡 Hint
Common Mistakes
Using mode "time" instead of "date".
Calling setDate with the event object instead of selectedDate.
5fill in blank
hard

Fill all three blanks to show the Picker with three items and update the selected value on change.

React Native
const [selected, setSelected] = useState("apple");

<Picker
  [3]=[1]
  onValueChange={value => [2]
>
  <Picker.Item label="Apple" value="apple" />
  <Picker.Item label="Banana" value="banana" />
  <Picker.Item label="Cherry" value="cherry" />
</Picker>
Drag options to blanks, or click blank then click option'
Aselected
BsetSelected(value)
CsetSelectedValue(value)
DselectedValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong setter function name like setSelectedValue instead of setSelected.
Confusing the prop name with the state variable.

Practice

(1/5)
1. What is the main purpose of a Picker component in React Native?
easy
A. To input text data
B. To display images in a gallery
C. To play audio files
D. To let users select one option from a list

Solution

  1. Step 1: Understand Picker usage

    A Picker is designed to show a list of options for the user to choose from.
  2. Step 2: Compare with other components

    Other options like text input or image gallery serve different purposes, not selection from a list.
  3. Final Answer:

    To let users select one option from a list -> Option D
  4. Quick 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
A. import { Picker } from 'react-native';
B. import Picker from '@react-native-picker/picker';
C. import { Picker } from '@react-native-picker/picker';
D. import Picker from 'react-native-picker';

Solution

  1. Step 1: Check official import syntax

    The Picker is exported as a named export from '@react-native-picker/picker', so curly braces are needed.
  2. Step 2: Eliminate incorrect imports

    Default import from the correct package or imports from wrong packages are incorrect.
  3. Final Answer:

    import { Picker } from '@react-native-picker/picker'; -> Option C
  4. Quick 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
A. Selected fruit: Banana
B. Selected fruit: undefined
C. Selected fruit: Cherry
D. Selected fruit: Apple

Solution

  1. Step 1: Understand state update on selection

    The onValueChange calls setFruit with the selected value, updating the fruit state.
  2. Step 2: Check selected value after choosing Banana

    When Banana is selected, fruit becomes 'Banana', so the Text shows 'Selected fruit: Banana'.
  3. Final Answer:

    Selected fruit: Banana -> Option A
  4. Quick 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
A. The 'value' prop should be 'date' instead of 'value'
B. The DatePicker component requires 'mode' prop
C. The 'onChange' callback parameters are incorrect
D. The 'setDate' function should not be called inside 'onChange'

Solution

  1. Step 1: Review DatePicker required props

    React Native DatePicker requires a 'mode' prop like 'date' or 'time' to work properly.
  2. Step 2: Check other props correctness

    'value' and 'onChange' usage is correct; 'setDate' is correctly called to update state.
  3. Final Answer:

    The DatePicker component requires 'mode' prop -> Option B
  4. Quick 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
A. Use separate state variables for country and birthdate, update each on change
B. Use a single state object and never update it
C. Only update state when the form is submitted
D. Use refs instead of state to track selections

Solution

  1. Step 1: Manage state for each input separately

    Use one state variable for country and another for birthdate to track changes independently.
  2. Step 2: Update state on user interaction

    Update the corresponding state in onValueChange for Picker and onChange for DatePicker to reflect UI changes.
  3. Final Answer:

    Use separate state variables for country and birthdate, update each on change -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard