There are many postings about react state update involving array, but none of them seem to address my case.
I'm using the FullCalendar.io component which accepts an array of events as a prop from the parent component, and populates the calendar cells. I tried to setState 'cal_events_state' based on nextProps.cal_events, but for some reason cal_event_state is set to [] (apparently the initial state value is overwritten).
What am I missing?
import React from 'react';
import ReactDOM from 'react-dom';
var $ = require ('jquery')
var Calendar = React.createClass ({
render: function() {
return <div id="calendar"></div>;
},
getInitialState: function() {
return {
cal_events_state: [{"due": "201505", "title": "Production"}]
, test_state: 11
}
},
componentDidMount: function() {
var self = this;
const var_cal_events = this.props.cal_events; //This prop is blank at this point because ajax hasn't returned an array yet.
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
events: var_cal_events;
})
},
componentWillReceiveProps: function(nextProps) {
var self = this;
var var_cal_events = nextProps.cal_events; // after receiving the ajax fetch payload, this has some event tasks.
this.setState({
cal_events_state: var_cal_events, // This doesn't update state with the newly received payload
test_state: 22 // This updates state
})
$('#calendar').fullCalendar({
events: var_cal_events
})
$('#calendar').fullCalendar('refetchEvents');
},
});