简体   繁体   English

如何在 React Native 中点击按钮打开 DatePicker

[英]how to open DatePicker on Button Click in React Native

I wanted to know how we can display datepicker on button click in react native.我想知道如何在 React Native 中点击按钮时显示日期选择器。 I have tried various solutions still no success,Any help would be helpful.我尝试了各种解决方案仍然没有成功,任何帮助都会有所帮助。 thank you.谢谢你。

Below is what i tried以下是我尝试过的

import React, { Component } from 'react';
import {
Platform,
StyleSheet,
View,DatePicker
} from 'react-native';

This is my render method这是我的渲染方法

render(){
    return(
        <TouchableOpacity
        onPress={() => this.datePicker.onPressDate()}>
        <Text>
         Hello Date
        </Text>
    </TouchableOpacity>
    
  <DatePicker
    style={{width: 200}}
    ref={(picker) => { this.datePicker = picker; }}
    date={this.state.date}
    mode="date"
    placeholder="Select date"
    format="YYYY-MM-DD"
   minDate="2016-05-01"
   maxDate="2020-12-12"
   confirmBtnText="OK"
   cancelBtnText="Cancel"
   onDateChange={(date) => {this.setState({date: date})}}
  
    />     
    );
}

whenever i run i get below error每当我跑步时,我都会遇到以下错误

You can simply have flag whether render picker component or not and switch it on Press.您可以简单地标记是否渲染选择器组件并将其打开按。 Something like this像这样的东西

render() {
  return (
    <View>
      <TouchableOpacity
        onPress={() => this.setState({ picker: !this.state.picker })}>
        <Text>Hello Date</Text>
      </TouchableOpacity>
      {this.renderPicker()}
    </View>
  );
}

renderPicker() {
  if (this.state.picker) {
    return (
      <DatePicker
        style={{ width: 200 }}
        ref={picker => {
          this.datePicker = picker;
        }}
        date={this.state.date}
        mode="date"
        placeholder="Select date"
        format="YYYY-MM-DD"
        minDate="2016-05-01"
        maxDate="2020-12-12"
        confirmBtnText="OK"
        cancelBtnText="Cancel"
        onDateChange={date => {
          this.setState({ date: date });
        }}
      />
    );
  }
}

I Suggest to use react-native-datepicker library for Date Picker.我建议为日期选择器使用react-native-datepicker库。 It will give you more customizing options and is compatible with both platforms ie Android and iOS.它将为您提供更多自定义选项,并与两个平台(即 Android 和 iOS)兼容。

Below is the sample code that i have used in one of my application:-以下是我在其中一个应用程序中使用的示例代码:-

<View style={{ backgroundColor: 'transparent', margin: 5 }}>
            <DatePicker date={this.state.date} showIcon={false} placeholder="Birthday" mode="date" format="DD-MM-YYYY"
              customStyles={{
                dateInput: {
                  borderWidth: 0,
                  height: 50,
                  width: 170,
                  right: 30,
                },
                dateText: {
                  marginTop: 5,
                  color: 'white',
                  fontSize: 18,
                },
                placeholderText: {
                  marginTop: 5,
                  right: 10,
                  color: 'white',
                  fontSize: 18,
                }
              }
              }
              onDateChange={(date) => { this.setState({ date: date }) }} placeholderTextColor="white" underlineColorAndroid={'rgba(0,0,0,0)'} style={{ height: 50, width: 170, paddingLeft: 15, borderRadius: 4, backgroundColor: 'rgba(0,0,0,0.4)' }}></DatePicker>
          </View>

You can find the library HERE你可以在这里找到图书馆

I am using react-native-datepicker and the following logic:我正在使用react-native-datepicker和以下逻辑:

<DatePicker
showIcon={false}
hideText={true}
ref={(ref)=>this.datePickerRef=ref}
...
/>

and from your element:并从您的元素:

onPress={() => this.datePickerRef.onPressDate()}

Give a look at this package react-native-modal-datetime-picker it's work for the cross platform and has a perfect design and highly customizable.看看这个包 react-native-modal-datetime-picker 它适用于跨平台并且具有完美的设计和高度可定制的。

https://github.com/mmazzarolo/react-native-modal-datetime-picker https://github.com/mmazzarolo/react-native-modal-datetime-picker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM