Datepicker日期不会随日期的setState更改

时间:2019-08-23 05:57:51

标签: react-native datepicker native-base

我正在使用我的native base datepicker开发一个React Native应用程序。在选择不同的日期时,会从api中获取并显示新数据。很好但是还有一个刷新按钮。如果按下该按钮,则应该显示初始api数据。现在,假设最初api是在获取8月21日的数据。然后从日期选择器中将日期更改为8月22日,并显示新数据。现在onPress刷新,它完美地显示了8月21日的初始数据,但在日期选择器中,日期仍然显示8月22日。我需要显示刷新的初始日期。

这是我现在拥有的代码:

class Third extends Component {
  constructor(props) {
    super(props);
    this.state = {
      first: '',
      second: '',
      third: '',
      date: '',
      draw: ''
    }
    this.setDate = this.setDate.bind(this);
  }

  async componentDidMount() {
    var today = new Date()
    var time = today.getHours()
    var weekDay = today.getDay()
   
    await fetch('https://api', {
      method: 'GET',
    })
      .then((response) => response.json())
      .then((response) => {
          this.setState({ tableData1: response.magnum })
          this.setState({ tableData2: response.special })
          this.setState({ tableData3: response.consolation })
          this.setState({ date: response.date })
          this.setState({ draw: response.draw })
      })
    }
    
  refreshData = async()=>{
    this.setState({pending: true})
    var today = new Date()
    var time = today.getHours()
    var weekDay = today.getDay()
   
    await fetch('https://api', {
      method: 'GET',
    })
      .then((response) => response.json())
      .then((response) => {
          this.setState({ tableData1: response.magnum })
          this.setState({ tableData2: response.special })
          this.setState({ tableData3: response.consolation })
          this.setState({ date: response.date })
          this.setState({ draw: response.draw })
      })
  }

  async setDate(newDate) {
    let day = newDate.getDate()
    let todate = newDate.getDate()
    let tomonth = newDate.getMonth()
    let toyear = newDate.getFullYear()
    let todday = toyear +'-'+ tomonth + '-' + todate
    let month = newDate.getMonth() + 1
    let year = newDate.getFullYear()
    day = String(day).length > 1 ? day : '0' + day
    month = String(month).length > 1 ? month : '0' + month
    let anday = year+'-'+month+'-'+day
    if (todday === anday){
      await fetch('https://api', {
        method: 'GET',
      })
        .then((response) => response.json())
        .then((response) => {
            this.setState({ tableData1: response.magnum })
            this.setState({ tableData2: response.special })
            this.setState({ tableData3: response.consolation })
            this.setState({ date: response.date })
            this.setState({ draw: response.draw })
        })
    }else{
    let fullDate = 'https://api/'+year+'-'+month+'-'+day
    console.log('date', fullDate)
    await fetch(fullDate, {
      method: 'GET',
      headers : { 
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      }
    })
      .then((response) => {
        if (response.status === 200) {
        console.log('response:',response)
        return response.json()
        }
      })
      .then((response) => {
        if (response === null || response === undefined ) {
          console.log('new:', response)
          this.setState({ tableData1: response.magnum })
          this.setState({ tableData2: response.special })
          this.setState({ tableData3: response.consolation })
          this.setState({ date: response.date })
          this.setState({ draw: response.draw })
      })
    }
  }

  render() {
    return (
          <View>
                  <DatePicker
                    defaultDate={this.state.date}
                    minimumDate={new Date(2018, 1, 1)}
                    maximumDate={new Date(2019, 12, 31)}
                    locale={"en"}
                    timeZoneOffsetInMinutes={undefined}
                    modalTransparent={false}
                    animationType={"fade"}
                    androidMode={"default"}
                    placeHolderText={this.state.date}
                    textStyle={{ color: "#000", fontSize: 18, paddingRight: 5 }}
                    placeHolderTextStyle={{ color: "#000", fontSize: 18, paddingRight: 5 }}
                    onDateChange={this.setDate}
                    disabled={false}
                  />
                <Icon name='refresh' onPress={()=>this.refreshData()} />

              </View>

1 个答案:

答案 0 :(得分:0)

我偶然发现了同一问题,无法更新datepicker中的日期,我所做的就是像这样向datepicker添加ref属性

<DatePicker
    defaultDate={this.state.date.toDate()}
    locale={"en"}
    timeZoneOffsetInMinutes={undefined}
    modalTransparent={false}
    animationType={"fade"}
    androidMode={"default"}
    placeHolderText={this.state.date.format('dddd L')}
    placeHolderTextStyle={{ padding: 0 }}
    textStyle={{ padding: 0 }}
    onDateChange={this.setDate}
    formatChosenDate={() => { return this.state.date.format('dddd L'); }}
    ref={c => this._datePicker = (c) } {# this line #} />

在选择器内部设置日期

if (this._datePicker) {
    this._datePicker.setDate(date);
}
相关问题