我很努力地更新事件prop更改後更新的jQuery fullcalendar。強制更新jQuery fullcalendar,在React.js中更改prop後
下面是一個包含數據&日曆類:
export default class Calendar extends Component {
constructor(props) {
super(props);
this.addEvent = this.addEvent.bind(this);
this.state = {
events: [
{
title: 'Sometitle',
date: moment(Date.now()),
allDay: false,
},
],
};
}
addEvent() {
const date = moment(Date.now()).add(1, 'hour');
this.setState({
events: [].concat(this.state.events, [{
title: 'Sometitle 1',
date,
allDay: false,
}]),
});
}
render() {
return (
<div className="allow-scroll">
<button onClick={this.addEvent}> Add event</button>
<EmployeesCalendar events={this.state.events} />
</div>
);
}
}
,這裏是包含fullcalendar插件類:
import React, { Component } from 'react';
import jQuery from 'jquery';
require('fullcalendar');
export default class EmployeesCalendar extends Component {
constructor(props) {
super(props);
this.state = {
events: props.events,
};
}
componentDidMount() {
const { fullCalendar } = this;
jQuery(fullCalendar).fullCalendar({
events: this.state.events,
});
}
componentWillReceiveProps(nextProps, nextState) {
const { fullCalendar } = this;
const { events } = nextState;
this.setState({
events: nextProps.events,
},() => {
console.log(this.state.events);
jQuery(fullCalendar).fullCalendar('refetchEventSources', nextProps.event);
});
}
componentWillUnmount() {
const { fullCalendar } = this;
jQuery(fullCalendar).fullCalendar('destroy');
}
render() {
return (
<div ref={(calendar) => { this.fullCalendar = calendar; }} />
);
}
}
我的addEvent功能,增加了新的事件數組和比我將它傳遞到EmployeesCalendar。
在EmployeesCalendar中,我更新componentWillReceiveProps
中的事件狀態,並將它傳遞給fullcalendar插件,強制更新它。但fullcalendar從不更新。
有什麼建議嗎?
已經嘗試了這個,'render','refetchEvents'事件也一樣 –