2

I use the Full Calendar of primereact, but I need to select a specific year and month. I have separate date picker for this purpose. But when I try to change date with gotoDate method of Full Calendar,

there is an error

'Uncaught TypeError: Cannot read property 'onViewDateChange' of null'

//method on datepicker change
  handleChange(date) {
    console.log(date);
    this.setState({
      startDate: date.value,
    });
      const calendarEl = document.getElementById('fullCalendar');
      const calendar = new Calendar(calendarEl);
      calendar.gotoDate(date.value);
      // calendar.gotoDate(this.state.startDate);
    }
  }
//datepicker
<Calendar view="month" dateFormat="mm/yy" value={startDate} onChange={this.handleChange} yearNavigator yearRange="2015:2030"/>
//calendar
 <FullCalendar
                          id="fullCalendar"
                          firstDay={1}
                          defaultView={`${CalendarRepo()
                            .isCalendarModelMonth(model) === 'month' ? 'dayGridMonth' : 'timeGridWeek'}`}
                          header={{
                            left: 'prev,next today, custom',
                            center: 'title',
                            right: 'dayGridMonth,timeGridWeek',
                          }}
                          customButtons={{
                            custom: {
                              text: 'custom 1',
                              click() {
                                calendar.gotoDate();
                                alert('clicked custom button 1!');
                              },
                            },
                          }}
                          nowIndicator
                          displayEventEnd={{
                            month: false,
                            basicWeek: true,
                            default: false,
                          }}
                          businessHours
                          timeZone="Europe/Kiev"
                          selectable
                          rerenderDelay={10}
                          eventDurationEditable
                          editable
                          droppable
                          eventDrop={(info) => {
                            this.handleDragEvent(info.event.id, info.event.title, info.event.start, info.event.end, info.event.allDay);
                          }}
                          plugins={[dayGridPlugin, timeGridPlugin, interactionPlugin, bootstrapPlugin, momentTimezonePlugin]}
                          themeSystem="bootstrap"
                          events={model.events}
                          eventReceive={this.eventReceive}
                          eventClick={this.handleEventClick}
                          dateClick={this.handleDateClick}
                          eventLimit
                        />
  • Sounds like an error in the `primereact` package or your `this.state.startDate` might be empty – Webdeveloper_Jelle Aug 27 '19 at 12:17
  • @Jbadminton it's not empty. here is console.log **Tue Aug 27 2019 15:48:28** of this before var calendar line. One of error links leads to my code with another line of code var calendarEl = document.getElementById('fullCalendar'); console.log(this.state.startDate); var calendar = **new primereact_calendar__WEBPACK_IMPORTED_MODULE_20__["Calendar"](calendarEl);** calendar.gotoDate(this.state.startDate); – Darina Tovstolies Aug 27 '19 at 12:51
  • @Jbadminton https://fullcalendar.io/docs/Calendar-gotoDate here link on documentation. I send date in different formats, but error is still the same. – Darina Tovstolies Aug 27 '19 at 13:10
  • I think its because the event is fired while the calendar is not yet rendered, can you also share more code please – Webdeveloper_Jelle Aug 27 '19 at 13:11
  • Can you try: `componentDidMount: function() { var calendarEl = document.getElementById('fullCalendar'); var calendar = new Calendar(calendarEl); calendar.gotoDate(this.state.startDate); },` – Webdeveloper_Jelle Aug 27 '19 at 13:11
  • @Jbadminton the problem is that i need to call this method in function after i choose the date i need in datepicker. Your code above is also not working in componentDidMount method with the same error. – Darina Tovstolies Aug 27 '19 at 13:19
  • Can you update your question with more info? more code so i can review your full code – Webdeveloper_Jelle Aug 27 '19 at 13:26
  • @Jbadminton updated. Please look again. – Darina Tovstolies Aug 27 '19 at 13:33
  • Looks ok, does the error only occur after the onclick or at the page load? – Webdeveloper_Jelle Aug 27 '19 at 13:40
  • @Jbadminton the error occur after the onclick event. – Darina Tovstolies Aug 27 '19 at 13:41

1 Answers1

8

Check out this codesandbox I made for you:
Codesandbox

This should help you with your project.
Maybe you should consider adding the event to a button outside of the calendar like in my sandbox.

import React from "react";
import FullCalendar from "@fullcalendar/react";
import dayGridPlugin from "@fullcalendar/daygrid";
import timeGridPlugin from "@fullcalendar/timegrid";
import interactionPlugin from "@fullcalendar/interaction"; // needed for dayClick

import "./styles.css";

// must manually import the stylesheets for each plugin
import "@fullcalendar/core/main.css";
import "@fullcalendar/daygrid/main.css";
import "@fullcalendar/timegrid/main.css";

export default class DemoApp extends React.Component {
  calendarComponentRef = React.createRef();

  state = {
    calendarWeekends: true,
    calendarEvents: [
      // initial event data
      { title: "Event Now", start: new Date() }
    ]
  };

  render() {
    return (
      <div className="demo-app">
        <div className="demo-app-top">
          <button onClick={this.toggleWeekends}>toggle weekends</button>&nbsp;
          <button onClick={this.gotoPast}>go to a date in the past</button>
          &nbsp; (also, click a date/time to add an event)
        </div>
        <div className="demo-app-calendar">
          <FullCalendar
           id="fullCalendar"
           firstDay={1}
            defaultView="dayGridMonth"
            header={{
              left: "prev,next today, custom",
              center: "title",
              right: "dayGridMonth,timeGridWeek,timeGridDay,listWeek"
            }}
            customButtons={{
              custom: {
                text: 'custom 1',
                click() {
                  this.gotoPast();
                },
              },
            }}
            plugins={[dayGridPlugin, timeGridPlugin, interactionPlugin]}
            ref={this.calendarComponentRef}
            weekends={this.state.calendarWeekends}
            events={this.state.calendarEvents}
            dateClick={this.handleDateClick}
          />
        </div>
      </div>
    );
  }

  toggleWeekends = () => {
    this.setState({
      // update a property
      calendarWeekends: !this.state.calendarWeekends
    });
  };

  gotoPast = () => {
    let calendarApi = this.calendarComponentRef.current.getApi();
    calendarApi.gotoDate("2019-10-12"); // call a method on the Calendar object
  };

  handleDateClick = arg => {
    if (confirm("Would you like to add an event to " + arg.dateStr + " ?")) {
      this.setState({
        // add new event data
        calendarEvents: this.state.calendarEvents.concat({
          // creates a new array
          title: "New Event",
          start: arg.date,
          allDay: arg.allDay
        })
      });
    }
  };
}
Webdeveloper_Jelle
  • 2,868
  • 4
  • 29
  • 55