简体   繁体   中英

airbnb/react-dates set blocked days after api fetch

Library : https://github.com/airbnb/react-dates

Question : Is possible to set blocked days after fetch from server? (wait/recall isDayBlocked callback or re-initialize calendar)

Problem : Callback 'isDayBlocked' is called after calendar changes view to next month when data are not ready yet.

Example Code:

import { DayPickerSingleDateController } from 'react-dates';

class DayPicker {

  isDayBlocked(day) {
    return this.days.has(day);
  }

  handleMonthChange() {
    // async api fetch
    this.days = getNextMonthBlockedDays();
  }

  render() { 
    <DayPickerSingleDateController
      {...someProps}
      isOutsideRange={this.isOutsideRange}
      onNextMonthClick={this.handleMonthChange}
    />
  }

}

What i tried :

  • mount/unmount calendar depending on loading prop (problem - animation to next month before nextMount callback + when calendar disappear it looks bad)

  • load to store nextMonth data, so when i change view to nextMonth 'isDayBlocked' works good and fetch data for nextMonth (problem - double click on nextMonth change or slow connection)

Any ideas please?

Holding your data in state would fix your issue and update your view everytime.

  handleMonthChange() {
    // async api fetch
    getNextMonthBlockedDays().then((data) => {
        this.setState({days : data});
    });
  }

You can also set a flag to not be able to change the month while a request is processing or you could cancel the previous request. (if your fetch api implements cancellation).

我在加载过程中添加了带有微调器的绝对叠加 div。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM