Maverick Maverick - 2 years ago 151
React JSX Question

react-big-calendar fetch events from remote url redux issue

I have a weird situation at hand while trying to make a demo appointment system.
I want to fetch events through Django backend rest api, which works totally fine and I could see the data coming in console as well.

Here is my index file (where provider is):

My calendar rendering component:

My Redux, its reduxsauce library I am using:

My ReduxSaga file:

Now, the thing is, I am struggling to do a few things after I fetch the events:

  • Where to setState, as it goes in infinite look if I call it in componentWillUpdate or componentDidUpdate?

  • if I use I get the following error which I have attached as a picture. It says
    uncaught at fetchEventsByDoctor The sort method cannot be invoked on an Immutable data structure.

    enter image description here

Now I know that props cant be modified and state could be.

Any suggestions what could be wrong? Thanks

Note: I am new to react and redux related technologies.
enter image description here


componentDidUpdate(prevProps, prevState){

if( !== {

let a =;
let p =
for(var i = 0; i < p.length; i++ ){
a[a.length] = p[i]

this.setState({events: a})


And that should do the trick, but Nagaraj's answer below is very much a good working solution too.

Answer Source

I have changed some things in your js file:

  1. Always use setState function to change the state and do not change the state elements directly, like is directly updating the state, which is wrong.
  2. Use componentWillReceiveProps to check, if the new props (nextProps) received is different than the old props (this.props), only then setState. Usually, when fetchEvent action is fired, set events to undefined and when you get the response, set it back to the api response in such case you can use:


if (! && {
  this.setState({ events: });

The Calender file:

import React from 'react';
import ReactDOM from 'react-dom';
import BigCalendar from 'react-big-calendar';
import events from './events';
import moment from 'moment';
import {
} from 'react-bootstrap';
import DateTime from 'react-datetime';
import {
} from 'react-redux';
import AppointmentActions from './Redux/AppointmentRedux';
import EventComponent from './Components/EventComponent';
import Halogen = require('halogen');

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

class Calendar extends React.Component {
  props: {
    handleAddCity: () => void,
    bookAppointmentForDoctor: () => void,
    fetchEvents: () => void,
    events: null

  constructor(props) {
    this.state = {
      date: new Date(),
      fromDate: null,
      toDate: new Date(),
      forValue: 15,
      showAddFormModal: false,
      hours: 12,
      minutes: 20,
      enabled: true,
      showNameError: '',
      showFromDateError: ''

  handleSelect(info) {

  onClick() {
    // Create a copy of the object before you change the state
    let events =;
      'title': 'some Party',
      'start': new Date(2017, 3, 15, 7, 0, 0).toLocaleString(),
      'end': new Date(2017, 3, 16, 10, 30, 0).toLocaleString()
    this.setState({ events });

  open() {
    this.setState({ showAddFormModal: true });

  close() {
    this.setState({ showAddFormModal: false });

  handleFromDateTimeChange(newDate) {
    return this.setState({ fromDate: newDate });

  handleToDateTimeChange(newDate) {
    return this.setState({ toDate: newDate });

  handleEventSelect(event, _this) {
    return (<Popover id="popover-positioned-right" title="Popover right">
        <strong>Holy guacamole!</strong> Check this info.

  handleMinutesSelect(_this) {
    this.setState({ forValue: });

  handleAddSubmitButton() {
    var flag = true
    const nameValue = ReactDOM.findDOMNode(;
    if (nameValue === '') {
      this.setState({ showNameError: "Please input a name. " });
      flag = false;

    if (this.state.fromDate === null) {
      this.setState({ showFromDateError: 'Please input right date and time' });
      flag = false;

    const fromDate = new Date(this.state.fromDate).toLocaleString();
    const toDate = moment(fromDate).add(this.state.forValue, 'm');
    const newToDate = new Date(toDate).toLocaleString();

    console.log(nameValue, this.state.forValue, fromDate, newToDate);

    // Create a copy of the object before you change the state
    let events =;
      'title': nameValue,
      'start': new Date(fromDate),
      'end': new Date(newToDate),
      'hexColor': '#FF5722',

    if (flag) {
      this.props.bookAppointmentForDoctor(3, nameValue, new Date(fromDate), new Date(newToDate))
      this.setState({ showAddFormModal: false });

  eventStyleGetter(event, start, end, isSelected) {
    var backgroundColor = event.hexColor;

    var style = {
      backgroundColor: backgroundColor,
      borderRadius: '0px',
      opacity: 0.8,
      color: 'black',
      border: '0px',
      display: 'block'

    return { style: style };

  componentDidMount() {

  componentWillReceiveProps(nextProps) {
    let oldEvents =;
    let newEvents =;

    // Check here if only newEvents is different than oldEvents and update state.
    // Like always set events as undefined if ajax request starts and set it back
    // Once you receive the data
    // if (newEvents <---> oldEvents ??? ) {
      this.setState({ events });
    // }

  render() {
    let { hours, minutes, enabled } = this.state;

    if (this.props.fetching) {
      return (<h1>loading.....</h1>)

    if ( !== null) {

      return (
        <div style={{height: 580}}>
          <h3 className="callout">Book appointment and events here.</h3>
            scrollToTime={new Date(1970, 1, 1, 6)}
            defaultDate={new Date()}
            onSelectEvent={(event, e) => this.handleEventSelect(event, e)}
            onSelectSlot={(slotInfo) => this.handleSelect(slotInfo)}
            eventPropGetter={(event) => this.eventStyleGetter(event)}
                toolbar: this.CustomToolbar,
                event: EventComponent,

            Add appointment

          <Modal show={this.state.showAddFormModal} onHide={this.close.bind(this)}>
              <Modal.Header closeButton>
                  <Modal.Title>Add Appointment</Modal.Title>
                         <FormGroup bsSize="large">
                            <FormControl type="text" placeholder="Enter Name" ref="name"/>{this.state.showNameError}
                          <ControlLabel>From (Date and Time)</ControlLabel>
                          <DateTime onChange={this.handleFromDateTimeChange.bind(this)}/>
                          { this.state.showFromDateError }
                          <HelpBlock>Please choose Date and Time of from when you want the appointment from,
                            both could be done using the same widget.</HelpBlock>
                       <FormGroup controlId="formControlsSelect">
                           <ControlLabel>For a period of</ControlLabel>
                           <FormControl componentClass="select" onChange={this.handleMinutesSelect.bind(this)} value={this.state.forValue}>
                              <option value="15">15 minutes</option>
                              <option value="30">30 minutes</option>
                              <option value="45">45 minutes</option>
                              <option value="60">60 minutes</option>
                              <option value="90">90 minutes</option>
                              <option value="120">2 hours</option>

                      <Button type="button" onClick={this.handleAddSubmitButton.bind(this)}>

    return (<h1>loading....</h1>);

const mapStateToProps = (state) => {
  return {
    fetching: state.appointment.fetching,

const mapDispatchToProps = (dispatch) => {
  return {
    handleAddCity: (city) => dispatch(AppointmentActions.setUserCity(city)),
    bookAppointmentForDoctor: (doctor_id, customer_name, from_date, to_date) =>
      dispatch(AppointmentActions.bookAppointmentForDoctor(doctor_id, customer_name, from_date, to_date)),
    fetchEvents: (doctor_id) =>

export default connect(mapStateToProps, mapDispatchToProps)(Calendar)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download