【问题标题】:react-big-calendar: Cannot read property 'momentLocalizer' of undefinedreact-big-calendar:无法读取未定义的属性“momentLocalizer”
【发布时间】:2018-06-26 22:45:39
【问题描述】:

在我的项目中,我试图初始化 react-big-calendar 但它说它不存在。这是来自控制台的错误:

home.tsx:18 Uncaught TypeError: Cannot read property 'momentLocalizer' of undefined
    at Object../src/main/webapp/app/modules/home/home.tsx (home.tsx:18)
    at __webpack_require__ (bootstrap:709)
    at fn (bootstrap:94)...

我检查了我的依赖项,甚至检查了我的 node_modules 文件夹中的库,它就在那里。还有其他事情发生。这是我的代码:

import './home.scss';

import * as React from 'react';
import { connect } from 'react-redux';
import { getSession } from 'app/shared/reducers/authentication';
import BigCalendar from 'react-big-calendar';
import moment from 'moment';

// Setup the localizer by providing the moment (or globalize) Object
// to the correct localizer.
BigCalendar.momentLocalizer(moment); // or globalizeLocalizer

export interface IHomeProp extends StateProps, DispatchProps {}


const MyCalendar = props => (
  <div>
    <BigCalendar
      events={[]}
      startAccessor='startDate'
      endAccessor='endDate'
      defaultDate={moment().toDate()}
    />
  </div>
);

export class Home extends React.Component<IHomeProp> {
  componentDidMount() {
    this.props.getSession();
  }

  render() {
    const { account } = this.props;
    return (
      <MyCalendar/>
    );
  }
}

const mapStateToProps = storeState => ({
  account: storeState.authentication.account,
  isAuthenticated: storeState.authentication.isAuthenticated
});

const mapDispatchToProps = { getSession };

type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;

export default connect(mapStateToProps, mapDispatchToProps)(Home);

这是我的软件包版本:

“反应”:“16.3.0”, “反应大日历”:“^0.19.1”, “时刻”:“2.22.0”,

有什么想法吗?

【问题讨论】:

    标签: reactjs react-big-calendar


    【解决方案1】:

    你可以试试这个: BigCalendar.setLocalizer(BigCalendar.momentLocalizer(moment));

    【讨论】:

      【解决方案2】:

      如果有人要添加此依赖项,必须小心某些属性的更改。 Bigcalendar 更改为 Calendar 和 momentLocalizer 必须从 react-big-calendar 导入。

      这是工作示例 =>

      import { Calendar, momentLocalizer } from "react-big-calendar";
      
      import moment from "moment";
      
      const localizer = momentLocalizer(moment);```
      
      
      
          <div style={{ height: 500 }}>
                    <Calendar
                      views={["month"]}
                      events={this.state.events}
                      defaultDate={new Date(Date.now())}
                      localizer={localizer}
                    />
                  </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-15
        • 1970-01-01
        • 2019-04-09
        • 2020-10-10
        • 2019-02-15
        相关资源
        最近更新 更多