2016-07-01 23 views
2

我有兩個問題,這是反應天然相關:更改變量的範圍與設定日期反應天然

1)如何訪問該變量沒有fromdate在_onPressButton()函數

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

回答

0

1)您可以使用this.state.fromDate來訪問它。

2)使用MomentJS。它使日期和時間的數學/顯示/轉換更容易。