reference-url: https://fullcalendar.io/docs/events-function
The below is a way to provide events as a function to to the FullCalendar. How would I write this in a react functional component, I fetch the data using graphql, which is a bit different.
function(info, successCallback, failureCallback) {
req.get('myxmlfeed.php')
.type('xml')
.query({
start: info.start.valueOf(),
end: info.end.valueOf()
})
.end(function(err, res) {
if (err) {
failureCallback(err);
} else {
successCallback(
Array.prototype.slice.call( // convert to array
res.getElementsByTagName('event')
).map(function(eventEl) {
return {
title: eventEl.getAttribute('title'),
start: eventEl.getAttribute('start')
}
})
)
}
})
and here's my current function, it returns a simple array.. how would I implement the callback and callback fail as they mentioned in the documents.
const fetchData = async () => {
const { searchEvents: myEvents } = await searchEvents()
try {
const data = myEvents.content.forEach((x) => {
x.start = new Date(x.start)
x.end = new Date(x.end)
})
setIsPageLoaded(true)
calendarRef.current.getApi().addEventSource(data)
return clonedata
} catch (err) {
console.log(err)
}
return true
}
forEachdoesn't return a value, sodatawill beundefined. The documentation says: "It is the event function’s responsibility to make suresuccessCallbackis being called with an array of parsable Event Objects." How doesfetchDatarelate to the first function? Can you provide a more complete example that shows yourfetchDataand the fullcalendar API interact with each other?