Basic usage with state
import React, {Component} from 'react'; import {View, Button, Platform} from 'react-native'; import DateTimePicker from '@react-native-community/datetimepicker'; export default class App extends Component { state = { date: new Date('2020-06-12T14:42:42'), mode: 'date', show: false, } setDate = (event, date) => { date = date || this.state.date; this.setState({ show: Platform.OS === 'ios' ? true : false, //*Question: Why true for ios? date, }); } show = mode => { this.setState({ show: true, mode, }); } datepicker = () => { this.show('date'); } timepicker = () => { this.show('time'); } render() { const { show, date, mode } = this.state; return ( <View> <View> <Button onPress={this.datepicker} title="Show date picker!" /> </View> <View> <Button onPress={this.timepicker} title="Show time picker!" /> </View> { show && <DateTimePicker value={date} mode={mode} is24Hour={true} display="default" onChange={this.setDate} /> } </View> ); } }
If Platform.OS === true
for ios, the datetimepicker window will always show and not be closed forever.
Why treat android and ios differently?
The code is from the official github so I guess it has some valid reason.