2

I have two questions, which are related to react-native:

1) how can i access the variable fromDate in _onPressButton() function

2) I want to increment the toDate by a day in getInitialState() function

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
  });
Community
  • 1
  • 1
sau123
  • 352
  • 5
  • 18

1 Answers1

0

1) You can access it using this.state.fromDate.

2) Use MomentJS. It makes math/displaying/conversion with dates and time much easier.

Denis Wells
  • 117
  • 10