简体   繁体   English

更改变量的范围并设置本机日期

[英]change the scope of variables & set date react-native

I have two questions, which are related to react-native: 我有两个问题,与本机反应有关:

1) how can i access the variable fromDate in _onPressButton() function 1)如何在_onPressButton()函数中访问fromDate变量

2) I want to increment the toDate by a day in getInitialState() function 2)我想在getInitialState()函数中将toDate增加一天

I have tried using this but didn't help. 我试过使用此方法,但没有帮助。

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
  });

1) You can access it using this.state.fromDate . 1)您可以使用this.state.fromDate访问它。

2) Use MomentJS . 2)使用MomentJS It makes math/displaying/conversion with dates and time much easier. 它使带有日期和时间的数学/显示/转换变得更加容易。

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

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