2
votes

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.

2

2 Answers

4
votes

Well, they have two appearance mode quite different to stick with native look and feel. Ios datetimepicker is a component that can be put and kept open inside the view (is made by a wheel) while android is more like a pop up so it has to be closed and opened only during user actions on its trigger.

Hope it will help Regards! Marco

0
votes

It's just because you want to show the picker in case the device is IOS.