【发布时间】:2021-06-19 08:13:43
【问题描述】:
我正在使用 NextJS 和 Fullcalendar。
我尝试使用fullcalendar 使用动态导入,例如this example(有关更多信息,此示例解决方案来自here)。
它成功了,但是有一个问题。几乎每 5 次刷新尝试中就有 1 次以错误 Please import the top-level fullcalendar lib before attempting to import a plugin 结束(like this,但在我的情况下版本是正确的)
在那之后,我发现 next/dynamic 的 modules 选项已被弃用。我认为这是我的问题的根源(我不确定 100%,但至少它已被弃用并且需要更新)。
正如docs 所说,处理动态导入的新方法是这样的:
const DynamicComponent = dynamic(() =>
import('../components/hello').then((mod) => mod.Hello)
)
但是因为我们需要多个导入,我找到了this solution。
目前,似乎一切正常,但我的代码无效。
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>;
}
我还找到了另一种使用next transpile modules 的方法。
但他们说"there is currently no way to transpile only parts of a package, it's all or nothing"。
事实上,我在next.config.js 中有一些东西。将此文件编辑为转译模块是另一种神秘的冒险,充满了问题。
我该怎么办?
【问题讨论】:
标签: reactjs fullcalendar next.js fullcalendar-5 react-loadable