2017-01-17 8 views
2

나는 ReactNative DatePickerAndroid Docs을 팔로우하고 있습니다. DatePickerIOS에는 onDateChange 메서드가 있습니다.ReactNative-DatePickerAndroid에서 선택한 날짜 가져 오기

DatePickerAndroid에서 선택한 날짜를 가져 오는 유사한 방법이 있습니까? 위의 문서에서는 선택한 날짜를 가져 오는 코드 예제를 언급하지 않았습니다.

당신은 주어진 코드 예제에서 showPicker 방법 날짜를 선택받을 수

답변

2

: 우리는, 문서의 예제 코드를 참조,이 기능은 약속 데이터를 처리 할 비동기 함수를 작성하고,이 기능을 실행할 수 있듯이

showPicker = async (stateKey, options) => { 
    try { 
     var newState = {}; 
     const {action, year, month, day} = await DatePickerAndroid.open(options); 
     if (action === DatePickerAndroid.dismissedAction) { 
     newState[stateKey + 'Text'] = 'dismissed'; 
     } else { 
     // <<<< Newly selected date >>>> 
     var date = new Date(year, month, day); 
     newState[stateKey + 'Text'] = date.toLocaleDateString(); 
     newState[stateKey + 'Date'] = date; 
     } 
     this.setState(newState); 
    } catch ({code, message}) { 
     console.warn(`Error in example '${stateKey}': `, message); 
    } 
    }; 
render() { 
    return (
    <TouchableWithoutFeedback 
    onPress={this.showPicker.bind(this, 'spinner', { date: this.state.presetDate })}> 
    <View> 
     <Text style={styles.text}>Date selector</Text> 
    </View> 
    </TouchableWithoutFeedback> 
) 
} 
2

당신에게 onPress 함수가있는 TextInput 또는 onFocus 함수가있는 TexInput. 예 :

async _onMyDatePress(){ 
    try { 
      const {action, year, month, day} = await DatePickerAndroid.open({   
       date: new Date() 
      }); 

      if(action == DatePickerAndroid.dateSetAction){ 
       console.log(year + ' ' + month + ' ' + day); 
      } 

     } catch ({code, message}) { 
      console.warn('Cannot open date picker', message); 
     } 
} 
관련 문제