【问题标题】:change the scope of variables & set date react-native更改变量的范围并设置日期 react-native
【发布时间】:2016-11-04 06:20:09
【问题描述】:

我有两个问题,和react-native有关:

1) 如何在 _onPressButton() 函数中访问变量 fromDate

2) 我想在 getInitialState() 函数中将 toDate 增加一天

我尝试过使用this,但没有帮助。

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

【问题讨论】:

    标签: javascript date scope datepicker react-native


    【解决方案1】:

    1) 您可以使用this.state.fromDate 访问它。

    2) 使用MomentJS。它使日期和时间的数学/显示/转换变得更加容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多