Javascript 改变变量的范围&;设置本机响应的日期
我有两个问题,与react native有关: 1) 如何在_onPressButton()函数中访问变量fromDate 2) 我想在getInitialState()函数中将toDate增加一天 我试过用,但没用Javascript 改变变量的范围&;设置本机响应的日期,javascript,date,scope,datepicker,react-native,Javascript,Date,Scope,Datepicker,React Native,我有两个问题,与react native有关: 1) 如何在_onPressButton()函数中访问变量fromDate 2) 我想在getInitialState()函数中将toDate增加一天 我试过用,但没用 var PerfTeam = React.createClass({ getInitialState(){ console.log("in return of getInitialState") return{ fromDate: new
var PerfTeam = React.createClass({
getInitialState(){
console.log("in return of getInitialState")
return{
fromDate: new Date(),
toDate: new Date(),
};
// toDate.setDate(fromDate.getDate()+1);
},
_onPressButton(){
console.log("onPress Button");
console.log(fromDate)
},
_fromDateChange(date){
this.setState({fromDate: date})
},
_toDateChange(date){
this.setState( { toDate: date } );
console.log("to date changed"+date)
},
render() {
return (
<View style={styles.container}>
<Text
style={[
{marginTop:40},
{fontSize: 20}
]}>
From
</Text>
<DatePickerIOS
style={[
{marginTop:10},
{height:100},
{width:400}
]}
date={this.state.fromDate}
mode='date'
onDateChange={this._fromDateChange}
/>
<Text
style={[
{marginTop:100},
{fontSize: 20}
]}>
To
</Text>
<DatePickerIOS
style={[
{height:100},
{width:400}
]}
date={this.state.toDate}
mode='date'
onDateChange={this._toDateChange}
/>
<TouchableHighlight
style={{marginTop: 120}}
onPress={this._onPressButton}>
<Text
style={[
{backgroundColor: 'black'},
{color:'white'},
{fontSize: 20}
]}>
Go
</Text>
</TouchableHighlight>
</View>
);
} // end of render
});
var PerfTeam=React.createClass({
getInitialState(){
log(“返回getInitialState”)
返回{
fromDate:新日期(),
toDate:新日期(),
};
//toDate.setDate(fromDate.getDate()+1);
},
_按按钮(){
console.log(“onPress按钮”);
console.log(fromDate)
},
_fromDateChange(日期){
this.setState({fromDate:date})
},
_toDateChange(日期){
this.setState({toDate:date});
console.log(“到更改日期”+日期)
},
render(){
返回(
从…起
到
去
);
}//渲染结束
});
1)您可以使用this.state.fromDate
访问它
2) 使用。它使计算/显示/转换日期和时间变得更加容易