2017-05-24 6 views
0

내가 원하는대로 날짜 형식을 지정하는 구성 요소를 만들었지 만 "react-native-modal-datetime-picker"를 사용하는 DateTimePicker와 다른 구성 요소를 만들었지 만 사용자가 모달을 표시하는 단추가 있습니다 날짜를 지정하면 이미 포맷 된 날짜와 그 날짜 위의 레이블이 버튼에 표시됩니다.내 구성 요소에서 값을 가져 오는 방법은 무엇입니까?

문제는 내보기에서 날짜 값이 필요하며 구성 요소에서 그 값을 가져 오는 방법을 모르겠다는 것입니다. (난 내 다른 구성 요소를 사용하고 있음을 참조)

import React from 'react'; 
import {Text} from 'react-native'; 

export default class DateFormatter extends React.Component { 
    formatDate(date){ 
    if(date==null) 
     if(this.props.text!=null) 
     return this.props.text; 
     else 
     return "Selecione uma data"; 
    else 
     return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear(); 
    } 

    render(){ 
    return(
     <Text style={this.props.style}> 
     {this.formatDate(this.props.date)} 
     </Text> 
    ); 
    } 
} 

내 InputDate :

import React from 'react'; 
import {View, TouchableOpacity, Text, Dimensions} from 'react-native'; 
import DateTimePicker from 'react-native-modal-datetime-picker'; 
import DateFormatter from '../components/DateFormatter'; 

export default class InputDate extends React.Component { 
    constructor(props){ 
    super(props); 
    this.state = { 
     isDateTimePickerVisible: false, 
     date: null, 
     showLabel: false 
    } 
    } 

    showDateTimePicker =() => { 
    this.setState({isDateTimePickerVisible: true}); 
    } 

    hideDateTimePicker=() => { 
    this.setState({isDateTimePickerVisible: false}); 
    } 

    handleDatePicked = (date) => { 
    this.setState({date: date}); 
    this.setState({showLabel: true}); 
    this.hideDateTimePicker(); 
    } 

    render(){ 
    return(
     <View style={{flex: 1}}> 
     <DateTimePicker isVisible={this.state.isDateTimePickerVisible} onConfirm={this.handleDatePicked} onCancel={() => this.setState({isDateTimePickerVisible: false})} /> 
     <Text style={{paddingBottom: 5}}>{this.state.showLabel ? "Data da Inspeção" : ""}</Text> 
     <TouchableOpacity style={{width: Dimensions.get('window').width * 0.9, height: 40}} onPress={() => this.setState({isDateTimePickerVisible: true})}> 
      <DateFormatter date={this.state.date} text={"Data da Inspeção"} style={this.state.date==null ? {fontSize: 16, paddingLeft: 5, color: "#575757"} : {fontSize: 16, paddingLeft: 5, color: "#000"}}/> 
      <View style={{backgroundColor: '#dfdfdf', alignSelf: 'stretch', height: 1, marginTop: 5}}></View> 
     </TouchableOpacity> 
     </View> 
    ); 
    } 
} 

그리고 내보기에

, 내가

내 DateFormatter는 (내가 텍스트의 값이 필요합니다) 단지 <InputDate />을 사용하면됩니다. InputDate 구성 요소에서 값을 가져와 내보기에서 어떻게 사용할 수 있습니까?

답변

1

국가/소도구를 통해 구성 요소를 통해 형식화 된 날짜 값을 올릴 필요가 있습니다. Date Formatter 구성 요소는 서식을 지정하고 해당 날짜에 저장하는 props 함수를 통해 입력 날짜 구성 요소에 값을 전달합니다. 이 메서드를 다시 사용하여 입력 날짜 부모 구성 요소 상태를 업데이트 할 수 있습니다.

날짜 포맷터

import React from 'react'; 
import {Text} from 'react-native'; 

export default class DateFormatter extends React.Component { 
    formatDate(date){ 
    if(date==null) 
     if(this.props.text!=null) 
     return this.props.text; 
     else 
     return "Selecione uma data"; 
    else 
     return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear(); 
    } 

    componentDidMount(){ 
     // Update parent component state 
     this.props.handleDateFormat(this.formatDate(this.props.date)); 
    } 

    render(){ 
    return(
     <Text style={this.props.style}> 
     {this.formatDate(this.props.date)} 
     </Text> 
    ); 
    } 
} 

입력 날짜

import React from 'react'; 
import {View, TouchableOpacity, Text, Dimensions} from 'react-native'; 
import DateTimePicker from 'react-native-modal-datetime-picker'; 
import DateFormatter from '../components/DateFormatter'; 

export default class InputDate extends React.Component { 
    constructor(props){ 
    super(props); 
    this.state = { 
     isDateTimePickerVisible: false, 
     date: null, 
     showLabel: false, 
     formattedDate: null 
    } 
    } 

    showDateTimePicker =() => { 
    this.setState({isDateTimePickerVisible: true}); 
    } 

    hideDateTimePicker=() => { 
    this.setState({isDateTimePickerVisible: false}); 
    } 

    handleDatePicked = (date) => { 
    this.setState({date: date}); 
    this.setState({showLabel: true}); 
    this.hideDateTimePicker(); 
    } 

    handleDateFormat = (formattedDate) => { 
     this.setState({ 
      formattedDate: formattedDate 
     }) 
    } 

    render(){ 
    return(
     <View style={{flex: 1}}> 
     <DateTimePicker handleDateFormat={this.handleDateFormat} isVisible={this.state.isDateTimePickerVisible} onConfirm={this.handleDatePicked} onCancel={() => this.setState({isDateTimePickerVisible: false})} /> 
     <Text style={{paddingBottom: 5}}>{this.state.showLabel ? "Data da Inspeção" : ""}</Text> 
     <TouchableOpacity style={{width: Dimensions.get('window').width * 0.9, height: 40}} onPress={() => this.setState({isDateTimePickerVisible: true})}> 
      <DateFormatter date={this.state.date} text={"Data da Inspeção"} style={this.state.date==null ? {fontSize: 16, paddingLeft: 5, color: "#575757"} : {fontSize: 16, paddingLeft: 5, color: "#000"}}/> 
      <View style={{backgroundColor: '#dfdfdf', alignSelf: 'stretch', height: 1, marginTop: 5}}></View> 
     </TouchableOpacity> 
     </View> 
    ); 
    } 
} 
+0

당신의 대답은 이해가되지 않습니다. 'handleDateFormat'을 DateTimePicker 구성 요소로 전달하지만 DateFormatter 구성 요소에서 'handleDateFormat'을 사용하고 있습니다. 그러나 좋아, 나는 모든 것을 올바른 장소에 놓았다. 그러나 나의 시야에서 어떻게 나의 데이트를 할 수 있을까? DateInput 구성 요소를 통해 날짜를 가져와야합니다. –

+0

우리는 'handleDateFormat' 함수를 DateTimePicker 구성 요소의 소품으로 전달합니다. 그런 다음 DateTimePicker 구성 요소 내부에서 해당 함수를 props에서 호출하고 형식이 지정된 날짜를 param으로 전달합니다. InputDate의 formattedDate 상태를 DateTimePicker가 전달한 것으로 설정하는 InputDate 구성 요소의 handleDateFormat 함수를 트리거합니다. 또 다른 옵션은 handleDateFormat 함수를 InputDate의 부모 구성 요소로 이동 한 다음 InputDate를 통해 전달하는 것입니다. DateTimePicker 및 같은 일을 (소품을 통해 부모의 상태를 업데이 트). –

관련 문제