I'm using NextJS and Fullcalendar.
I tried to use fullcalendar
using dynamic import like in this example(for more info, this example solution comes from here).
It worked, But there was a problem. Almost every 1 out of 5 refreshing attempts ended in error Please import the top-level fullcalendar lib before attempting to import a plugin
(like this, but versions are correct in my case)
After that, I found out that the modules option for next/dynamic has been deprecated. I thought it was a source of my problem (I'm not sure 100%, but at least it was deprecated and needed to be updated).
As docs says, newer way to handle dynamic imports is like this:
const DynamicComponent = dynamic(() =>
import('../components/hello').then((mod) => mod.Hello)
)
But because we need multiple imports I found this solution.
For now, It seems like everything should be working, but my code has no effect.
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import "@fullcalendar/common/main.css"; // @fullcalendar/react imports @fullcalendar/common
import "@fullcalendar/daygrid/main.css"; // @fullcalendar/timegrid imports @fullcalendar/daygrid
import "@fullcalendar/timegrid/main.css"; // @fullcalendar/timegrid is a direct import
import "./Fullcalendar.module.scss";
let CalendarComponent;
const Calendar = dynamic(() =>
import("@fullcalendar/react").then((module) => module.Calendar)
);
const dayGridPlugin = dynamic(() =>
import("@fullcalendar/daygrid").then((module) => module.dayGridPlugin)
);
export default function FullCalendar(props) {
const [calendarLoaded, setCalendarLoaded] = useState(false);
useEffect(() => {
CalendarComponent = () => (
<Calendar
{...props}
plugins={[dayGridPlugin]}
initialView="dayGridMonth"
/>
);
setCalendarLoaded(true);
}, []);
let showCalendar = (props) => {
if (!calendarLoaded) return <div>Loading ...</div>;
return <CalendarComponent {...props} />;
};
return <div>{showCalendar(props)}</div>;
}
I also find another way to use next transpile modules.
But they say that "there is currently no way to transpile only parts of a package, it's all or nothing"
.
In fact, I have something in next.config.js
. Editing this file into transpile modules is another mysterious adventure, full of problems.
What should I have to do?