我使用react native Time picker作为日期组件。当我使用它更新时间时,它无法在变量(cdate)中获取和保存时间
道具
constructor (props) {
super(props);
this.state = {
cdate: '',
setDate: '',
text: '',
isVisible: false,
setVisible: false,
}
渲染中
const { cdate, isVisible, setDate, setVisible } = this.state;
时间选择器
<Button title={cdate} onPress={() => this.setState({ setVisible: true })} />
< DateTimePicker
mode="time"
isVisible={setVisible}
cdate={cdate}
onConfirm={cdate => {
this.setState({setDate:cdate.toTimeString(),setVisible:false})
}}
onCancel={() => this.setState({ setVisible: false })}
/>
我希望按钮在设置时间后在其标题中显示设置的时间
答案 0 :(得分:0)
您尝试使用以下代码:
handleDatePicked = date => {
console.log("A date has been picked: ", date);
this.setState({setDate:date.toTimeString(),setVisible:false})
};
...
<Button title={cdate} onPress={() => this.setState({ setVisible: true })} />
< DateTimePicker
mode="time"
isVisible={setVisible}
onConfirm={this.handleDatePicked}
onCancel={() => this.setState({ setVisible: false })}
/>