How to set momentLocalizer (moment.js) for react-big-calendar (fullcalendar.js)?

纵饮孤独 提交于 2021-02-19 01:24:18

问题


I'm using react-big-calendar And need help to implement localization which is required The example from git

import BigCalendar from 'react-big-calendar';
import moment from 'moment';

BigCalendar.setLocalizer(
  BigCalendar.momentLocalizer(moment)
);

My code

var moment = require('moment');
var momentLocalizer = require('react-widgets/lib/localizers/moment');
import BigCalendar from 'react-big-calendar';

BigCalendar.setLocalizer(
    BigCalendar.momentLocalizer(moment.locale('en'))
);

let MyCalendar = props => (
    <div>
        <BigCalendar

        />
    </div>
);

Whatever I tried it doesn't work

Uncaught TypeError: moment is not a function


回答1:


My code below, it works for me. I simply set the culture prop on my BigCalendar if I want to change localization.

import React, { Component } from 'react';
import 'react-big-calendar/lib/css/react-big-calendar.css'
import BigCalendar from 'react-big-calendar';
import moment from 'moment';

BigCalendar.momentLocalizer(moment);

class Calendar extends Component {
    constructor(props, context) {
      super(props, context);
    }

    render() {
      return (
        <div>
          <BigCalendar
            culture='en-GB'
            events={this.props.tasks}
            views={['month', 'week']}/>
        </div>
      );
    }
  }



回答2:


I had to import the locale I wanted to get the calendar in the correct locale.

Notice the import 'moment/locale/nb';.

import React, { Component } from 'react';
import BigCalendar from 'react-big-calendar';
import moment from 'moment';
import 'moment/locale/nb';
import 'react-big-calendar/lib/css/react-big-calendar.css';

BigCalendar.setLocalizer(BigCalendar.momentLocalizer(moment));

class Calendar extends Component {
  constructor() {
    super();
    this.state = {
      events: [],
    };
  }

  render() {
    return (
      <div className="Calendar">
        <BigCalendar
          events={this.state.events}
        />
      </div>
    );
  }
}

This is for version 0.17.0 of react-big-calendar.



来源:https://stackoverflow.com/questions/36514480/how-to-set-momentlocalizer-moment-js-for-react-big-calendar-fullcalendar-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!