未捕获的TypeError:无法读取未定义的属性“ setLocalizer”

时间:2020-02-13 10:19:14

标签: reactjs react-big-calendar

import Calendar from 'react-big-calendar'
Calendar.setLocalizer(Calendar.momentLocalizer(moment))

使用插件版本:

"moment": "^2.24.0"
"react-big-calendar": "^0.23.0"

更新大日历程序包后,出现此错误。更新后,我已经多次安装了该软件包。

但是收到此错误"Uncaught TypeError: Cannot read property 'setLocalizer' of undefined "

我已经阅读了与"Cannot read property 'momentLocalizer' of undefined "相关的答案,但是它对我不起作用。请帮忙。

预先感谢

2 个答案:

答案 0 :(得分:2)

这将起作用:

import { Calendar, momentLocalizer } from 'react-big-calendar'
import moment from 'moment'

const localizer = momentLocalizer(moment)

const MyCalendar = props => (
  <div>
    <Calendar
      localizer={localizer}
      events={myEventsList}
      startAccessor="start"
      endAccessor="end"
      style={{ height: 500 }}
    />
  </div>
)

答案 1 :(得分:1)

您需要执行以下操作:

import { Calendar, momentLocalizer } from 'react-big-calendar';
import moment from 'moment';

const localizer = momentLocalizer(moment);

在您的JSX中:

  <div style={{ height: '500pt'}}>
              <Calendar
                 ....
                defaultDate={moment().toDate()}
                localizer={localizer}
              />
            </div>

希望对您有帮助!