【问题标题】:Warning: Failed prop type: The prop `date` is marked as required in `CalendarProvider`, but its value is `undefined`警告:失败的道具类型:道具“日期”在“日历提供者”中标记为必需,但其值为“未定义”
【发布时间】:2020-07-05 18:35:12
【问题描述】:

我是 react-native 应用程序的新手,我试图获取议程选定的日期数据,但我不能接受它,当我调试它时,它正在写入道具日期已标记但未定义。而且我无法获取选定的日期数据。这是我的议程屏幕代码:


export default class AgendaScreen extends Component {
  constructor(props) {
    super(props);
    this.state = {
      markedDates: this.getMarkedDates(),
      isVisibleItems: false,
      onDayPress: { dateString: moment(new Date()).format("YYYY-MM-DD") }
    };
  }
  buttonPressed() {
    Alert.alert("show more");
  }

  getMarkedDates = () => {
    const { items } = this.props;
    const marked = {};
    items.calendar
      ? items.calendar.forEach(item => {
        // only mark dates with data
        if (
          item.data &&
          item.data[1].limit == 0 &&
          item.data[0].limit == 0 &&
          !_.isEmpty(item.data[0])
        ) {
          marked[item.title] = { marked: true };
        }
      })
      : null;
    return marked;
  };

  componentDidMount() {
    this.setState({ isVisibleItems: true });
  }
  render() {
    const { items } = this.props;
    const { isVisibleItems, onDayPress } = this.state;
    return (
      <View style={{ marginTop: 15, zIndex: 0 }}>
        <CalendarProvider >
          <ExpandableCalendar
            firstDay={1}
            markedDates={this.getMarkedDates()}
            theme={this.getTheme()}
            minDate={new Date()}
            onDayPress={day => {
              this.setState({ isVisibleItems: true, onDayPress: day });
            }}
          />
        </CalendarProvider>
        {isVisibleItems ? this.renderItem(onDayPress) : null}
      </View>
    );
  }
}

看起来我的 getMarkedDates 函数不起作用 不知道为什么

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    看起来在您的 CalendarProvider 组件中您已根据需要设置日期,现在您应该像 &lt;CalendarProvider date = {date} &gt; 一样提供它,还尝试在 componentDidMount 而不是构造函数中调用 getMarkedDates 函数。

    【讨论】:

      猜你喜欢
      • 2017-09-01
      • 2019-06-25
      • 2018-11-23
      • 1970-01-01
      • 2018-09-22
      • 2017-12-11
      • 2020-12-24
      • 1970-01-01
      • 2022-07-20
      相关资源
      最近更新 更多