这是我关于react native的第一个问题。我作为初学者创建React Native应用程序。
我正在使用“react-native-datepicker”(https://github.com/xgfe/react-native-datepicker)获取子组件中的日期,我想将此日期存储到Firebase中。但是,我无法获取每个用户在picker上输入的日期。
那么,如何做到这一点?
下面一个是父组件
class PersonalInfo1 extends React.Component {
state = {
fromTermOfMaridge: '',
ToTermOfMaridge: '',
}
handleOnpress() {
const db = firebase.firestore();
const { currentUser } = firebase.auth();
db.collection(`users/${currentUser.uid}/form1`).add({
form1 :
[
{ fromTermOfMaridge: this.state.fromTermOfMaridge },
{ ToTermOfMaridge: this.state.ToTermOfMaridge },
]
})
.then(() => {
this.props.navigation.goBack();
})
.catch((error) => {
console.log(error);
});
}
render() {
return (
<ScrollView style={styles.container}>
<InfoHeader
navigation={this.props.navigation}>申請者情報1
</InfoHeader>
<Notes />
<View style={styles.questionTextBoxDateMargin}>
<QuestionTextBoxDate
onDateChange={(date) =>
{ this.setState({ fromTermOfMaridge: date }); }}>
婚姻期間(上記で既婚・離婚と答えた方)
</QuestionTextBoxDate>
<QuestionTextBoxDate onDateChange={(date) =>
{ this.setState({ ToTermOfMaridge: date }); }}>
から
</QuestionTextBoxDate>
</View>
<SubmitButton
style={styles.saveButton}
onPress={this.handleOnpress.bind(this)}>
保存
</SubmitButton>
<Copyrights />
</ScrollView>
);
}
}
下一个是子组件
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import DatePicker from 'react-native-datepicker';
class QuestionTextBoxDate extends React.Component {
constructor(props) {
super(props)
this.state = {
date : '',
};
}
render() {
return (
<View style={styles.container}>
<View style={styles.questionTextBox}>
<Text style={styles.questionText}>
{this.props.children}
</Text>
</View>
<DatePicker
style={styles.datePicker}
date={this.state.date}
mode="date"
locale="ja"
placeholder="年月日を入力"
format="YYYY年MM月DD日"
minDate="1900-01-01"
maxDate="2020-01-01"
confirmBtnText="確定"
cancelBtnText="閉じる"
customStyles={{
dateInput: {
height: 30,
backgroundColor: '#F4F4F4',
borderWidth: 1,
borderRadius: 6,
borderColor: '#707070',
}
}}
showIcon={false}
onDateChange={(date) => { this.setState({ date }); }}
/>
</View>
);
{\fnSimHei\bord1\shad1\pos(200,288)}
2条答案
按热度按时间ymzxtsji1#
在DatePicker组件中,将onDateChange方法更改为以下方法
vkc1a9a22#