Dynamically importing functionality. Wait with component render.
Unanswered
Orinoco Crocodile posted this in #help-forum
Orinoco CrocodileOP
Hello!
I'm utilizing Nivo (a graph library) in Nextjs:
https://nivo.rocks/
Nivo was built on "old standards" so to render the graphs I'm doing:
Which works fine.
However Nivo also has a "
How do I go about doing this the best way? I pretty much want to block rendering the component until
I'm utilizing Nivo (a graph library) in Nextjs:
https://nivo.rocks/
Nivo was built on "old standards" so to render the graphs I'm doing:
const ResponsiveLine = dynamic(
() => import("@nivo/line").then(({ ResponsiveLine }) => ResponsiveLine),
{ ssr: false },
);
export default () => <ResponsiveLine>...</ResponsiveLine>Which works fine.
However Nivo also has a "
@nivo/core" package from which I'd like to extract the "linearGradientDef"-function.How do I go about doing this the best way? I pretty much want to block rendering the component until
linearGradientDef is available.5 Replies
Orinoco CrocodileOP
Sidenote: the project is running the old
/pages router rather than /app. Had it been /app I'm pretty sure I could just:export default async () => {
const { linearGradientDef } = await import("@nivo/core");
return <ResponsiveLine defs={[linearGradientDef(...yadayada
}@Orinoco Crocodile Sidenote: the project is running the old `/pages` router rather than `/app`. Had it been `/app` I'm pretty sure I could just:
export default async () => {
const { linearGradientDef } = await import("@nivo/core");
return <ResponsiveLine defs={[linearGradientDef(...yadayada
}
try it with the
https://codesandbox.io/p/devbox/nextjs-dynamic-import-with-react-icons-zk1kz9?file=%2Fcomponents%2FDynamicIcon.tsx
It should be easy to make a functional version from it, that suites your case
dynamic function from nextjs. It should work here as well. This example shows the dynamic imports from react icons in pages router:https://codesandbox.io/p/devbox/nextjs-dynamic-import-with-react-icons-zk1kz9?file=%2Fcomponents%2FDynamicIcon.tsx
It should be easy to make a functional version from it, that suites your case
Orinoco CrocodileOP
The dynamic function from nextjs are meant for Components as far as I know? I'm trying to import a function from a package (oh and I tried it and it didn't work)
oh ok :/
Orinoco CrocodileOP
I found a ghetto solution:
export default () => {
useEffect(() => {
(async () => {
const nivoCore = await import("@nivo/core");
setLinearGradientDef(
nivoCore.linearGradientDef("gradientA", [
{ offset: 0, color: "inherit" },
{ offset: 100, color: "inherit", opacity: 0 },
]),
);
})();
}, []);
if (!linearGradientDef) return null;
// Don't call linearGradientDef as a function - it was already
return <ResponsiveLine defs={[linearGradientDef]}
}