Reader small image

You're reading from  React Native By Example

Product typeBook
Published inApr 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781786464750
Edition1st Edition
Languages
Right arrow
Author (1)
Richard Kho
Richard Kho
author image
Richard Kho

Richard Kho is a software engineer living in San Francisco. He taught himself how to code in 2014 and has lived a past life as a photographer and cinematographer. He currently works for Capital One and has taught software engineers at Hack Reactor in the past. Richard is also a technical advisor to Code Chrysalis, an advanced software engineering immersive in Tokyo.
Read more about Richard Kho

Right arrow

DatePickerAndroid and TimePickerAndroid


Setting a time and date on Android is much different from iOS. With iOS, you have a DatePickerIOS component that includes both the date and time. On Android, this is split into two native modals, DatePickerAndroid for the date and TimePickerAndroid for the time. It's not a component to render either, it's an asynchronous function that opens the modal and waits for a natural conclusion before applying logic to it.

To open one of these, wrap an asynchronous function around it:

async renderDatePicker () { 
  const { action, year, month, day } = await DatePickerAndroid.open({ 
    date: new Date() 
  }); 

  if (action === DatePickerAndroid.dismissedAction) { 
    return; 
  } 

  // do something with the year, month, and day here 
} 

Both the DatePickerAndroid and TimePickerAndroid components return an object, and we can grab the properties of each object by using ES6 destructuring assignment, as shown in the preceding snippet.

As these components will render...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
React Native By Example
Published in: Apr 2017Publisher: PacktISBN-13: 9781786464750

Author (1)

author image
Richard Kho

Richard Kho is a software engineer living in San Francisco. He taught himself how to code in 2014 and has lived a past life as a photographer and cinematographer. He currently works for Capital One and has taught software engineers at Hack Reactor in the past. Richard is also a technical advisor to Code Chrysalis, an advanced software engineering immersive in Tokyo.
Read more about Richard Kho