【问题标题】:React fullCalendar, reaching this.props in eventReceive functionReact fullCalendar,在 eventReceive 函数中到达 this.props
【发布时间】:2020-10-05 22:43:11
【问题描述】:

我想用我的事件的开始时间设置我父母的属性之一,但我无法从 function(info) 方法内部访问 this.props。我怎样才能达到我想使用的方法?

eventReceive={function (info) {
                this.props.onChangeStartDate(info.start);
              }}

onChangeStartDate 是我父母的函数之一,它在父组件中设置约会的开始日期,我想将我刚刚添加到日历中的事件数据作为该函数的参数提供。

我收到以下错误:

无法读取未定义的属性“onChangeAppointment”

感谢您的帮助。

【问题讨论】:

  • function (info) {更改为(info) => {,否则this将引用当前函数,而不是存在于该函数之外的this
  • 嗨,谢谢你的帮助,看起来解决了这个问题,但是现在 info.start 返回未定义,但如果我只输入 function(info){console.log(info.start)} 它完美显示日期。可能是什么问题?
  • 不确定,看看传入的内容 - 在函数中 console.log(info) this.props.onChangeStartDate 之前执行

标签: javascript reactjs fullcalendar parameter-passing react-props


【解决方案1】:

以下将解决您的错误。

eventReceive={(info) => {
    this.props.onChangeStartDate(info.start);
}}

在哪里 () => {} 是常用的箭头函数。见documentation

发生错误的原因是因为您在函数范围内丢失了 this 的上下文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2017-08-18
    相关资源
    最近更新 更多