reactjs - 在 ReactJS 中访问完整的日历 API
问题描述
如何访问完整的日历 API,以便我可以操作和使用 setDates、moveStart 等方法。我想使用他们在此页面中提供的方法。
https://fullcalendar.io/docs/Event-setDates
问题:提供的完整日历示例是基于类的并且已过时。如何在功能组件中使用这些方法..
export default class DemoApp extends React.Component {
calendarRef = React.createRef()
render() {
return (
<FullCalendar ref={this.calendarRef} plugins={[ dayGridPlugin ]} />
)
}
someMethod() {
let calendarApi = this.calendarRef.current.getApi()
calendarApi.next()
}
}
到目前为止我所做的目前我在handleEventAdd 和handleUpdate 函数中使用引用,我接受事件并相应地操作它。
<FullCalendar
nowIndicator
plugins={[momentTimezonePlugin, timeGridPlugin, dayGridPlugin, interactionPlugin]}
initialView="timeGridWeek"
headerToolbar={{
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay',
}}
timeZone={userTimezone || Intl.DateTimeFormat().resolvedOptions().timeZone}
ref={calendarRef}
editable
selectable
select={handleEventAdd}
eventResize={(e) => handleupdateSchedule(e)}
/>
什么有效:
const CalAPI = e.view.calendar
当我这样做时,我可以访问日历方法,例如
Calendar::getEvents
Calendar::getEventById
Calendar::addEvent
我像这样使用它们
CalAPI.getEventById(e.event.id).remove() //works
但我无法访问其他方法,例如
Event::setProp
Event::setExtendedProp
Event::setStart
Event::setEnd
Event::setDates
以上都行不通。请在此处找到我要使用的事件方法列表https://fullcalendar.io/docs/Event-setDates
更新: 发布有关功能的更多信息。
select={Addevent} // works even when I don`t pass the "e"
eventClick={(e) => Deleteevent(e)} // does not find CalAPI reference
eventDrop={(e) => Updateevent(e)} // does not find CalAPI
eventResize={(e) => Updateevent(e)} // the calendar event refreshes automatically I dont need the CalAPI here.
解决方案
最后经过大量的工作,解决方案其实很简单,我们在使用功能组件时应该使用 useRef 钩子。然后从 yourRefname.current.getAPI()... 访问 CalAPI... 从这里您可以访问所有需要的方法。
PS:感谢@ADyson,我离答案更近了。
解决方案
推荐阅读
- vue.js - 避免直接改变 prop,因为每当父组件重新渲染时,该值都会被覆盖
- javascript - 为什么固定列不能使用数据表工作?
- java - 在运行时获取错误 jar 不存在
- api - 如何在 SOAP Web 服务中设计标头信息?
- jboss - 是否可以在 jBPM 中集成 Salesforce 或 facebook
- mule - mule 中 APIkit 和 APIkit 控制台组件的区别?
- javascript - 如果选中复选框,如何更改背景颜色,由 id 选择
- ios - 具有动态高度的 TableViewHeader
- python - 无法解码 JSON 对象,但输入为 JSON 格式
- yaml - YAML 用于字符串内的 tmuxinator 映射值