해결책은 내부적으로 DatePicker를 나타내는 함수를 호출하는 것입니다.
SignUp.js
<View>
<Text>Apellido</Text>
<TextInput ref="lastname" onSubmitEditing={() => this.refs.date.showPicker()} onChangeText={(lastname) => this.setState({ lastname })} returnKeyType="next" />
</View>
<View>
<Text>Fecha nacimiento</Text>
<DatePicker
ref="date"
date={this.state.date}
onDateChange={(date) => this.setState({ date })}
/>
</View>
DatePicker.js
import React, { Component } from 'react';
import moment from "moment";
import {
DatePickerAndroid,
Text,
TouchableWithoutFeedback,
View,
} from 'react-native';
export default class DatePicker extends Component {
constructor(props) {
super(props);
this.state = {
date: props.date,
text: moment(props.date).format('DD/MM/YY'),
maxDate: props.maxDate,
minDate: props.minDate,
};
}
async showPicker() {
try {
const {action, year, month, day} = await DatePickerAndroid.open(
{ date: this.props.date, maxDate: this.props.maxDate, minDate: this.props.minDate }
);
if (action !== DatePickerAndroid.dismissedAction) {
var date = new Date(year, month, day);
this._onDateChange(date);
}
} catch ({code, message}) {
console.warn('Error in example ' + message);
}
}
_onDateChange(date) {
var text = moment(date).format('DD/MM/YY');
this.props.onDateChange(date);
this.setState({
text,
date
});
}
render() {
return (
<TouchableWithoutFeedback onPress={() => this.showPicker()}>
<View >
<Text style={this.props.style}>{this.state.text}</Text>
</View>
</TouchableWithoutFeedback>
);
}
}
DatePicker.defaultProps = {
date: new Date(),
minDate: new Date(1900, 1, 1),
maxDate: new Date(2016, 11, 1)
};
는 http://stackoverflow.com/questions/37949981/call-child-method-from-parent ..이 무엇인지 를 찾고 있습니다? – semuzaboi
Yees, thaks you !! –
wc ... 당신은 대답하고 닫을 수 있습니다 :)! – semuzaboi