Dynamic Imports - NextJS
I have a simple function which loads a script:
const creditCardScript = (
onReadyCB,
onErrorCB,
) => {
let script = document.createElement("script");
script.type = "text/javascript";
script.src = process.CREDIT_CARD_SCRIPT;
document.head.appendChild(script);
script.onload = function() {
...
};
};
export default creditCardScript;
Before I migrated to NextJS, I was importing the script with: import creditCardScript from "./creditCardScript"
.
Sine NextJS renders components server side in Node, care needs to be taken to ensure that any code with a reference to window
(which is browser specific), doesn't get called until componentDidMount
.
NextJS solves this issue by providing dynamic imports (a wrapper around react-loadable) which:
- only load the component when needed,
- provides an option to only load the component on client side
(ssr: false
).
I went ahead and implemented dynamic imports:
const creditCardScript = dynamic(import("./creditCardScript"), { ssr: false });
In componentDidMount
:
componentDidMount = () => {
creditCardScript(
this.onReadyCB,
this.onErrorCB
);
};
But I'm getting this:
Uncaught TypeError: Cannot call a class as a function
I've tried to convert the function to a class and use the constructor to pass in args
, but my code now fails silently.
javascript node.js reactjs next.js react-loadable
add a comment |
I have a simple function which loads a script:
const creditCardScript = (
onReadyCB,
onErrorCB,
) => {
let script = document.createElement("script");
script.type = "text/javascript";
script.src = process.CREDIT_CARD_SCRIPT;
document.head.appendChild(script);
script.onload = function() {
...
};
};
export default creditCardScript;
Before I migrated to NextJS, I was importing the script with: import creditCardScript from "./creditCardScript"
.
Sine NextJS renders components server side in Node, care needs to be taken to ensure that any code with a reference to window
(which is browser specific), doesn't get called until componentDidMount
.
NextJS solves this issue by providing dynamic imports (a wrapper around react-loadable) which:
- only load the component when needed,
- provides an option to only load the component on client side
(ssr: false
).
I went ahead and implemented dynamic imports:
const creditCardScript = dynamic(import("./creditCardScript"), { ssr: false });
In componentDidMount
:
componentDidMount = () => {
creditCardScript(
this.onReadyCB,
this.onErrorCB
);
};
But I'm getting this:
Uncaught TypeError: Cannot call a class as a function
I've tried to convert the function to a class and use the constructor to pass in args
, but my code now fails silently.
javascript node.js reactjs next.js react-loadable
add a comment |
I have a simple function which loads a script:
const creditCardScript = (
onReadyCB,
onErrorCB,
) => {
let script = document.createElement("script");
script.type = "text/javascript";
script.src = process.CREDIT_CARD_SCRIPT;
document.head.appendChild(script);
script.onload = function() {
...
};
};
export default creditCardScript;
Before I migrated to NextJS, I was importing the script with: import creditCardScript from "./creditCardScript"
.
Sine NextJS renders components server side in Node, care needs to be taken to ensure that any code with a reference to window
(which is browser specific), doesn't get called until componentDidMount
.
NextJS solves this issue by providing dynamic imports (a wrapper around react-loadable) which:
- only load the component when needed,
- provides an option to only load the component on client side
(ssr: false
).
I went ahead and implemented dynamic imports:
const creditCardScript = dynamic(import("./creditCardScript"), { ssr: false });
In componentDidMount
:
componentDidMount = () => {
creditCardScript(
this.onReadyCB,
this.onErrorCB
);
};
But I'm getting this:
Uncaught TypeError: Cannot call a class as a function
I've tried to convert the function to a class and use the constructor to pass in args
, but my code now fails silently.
javascript node.js reactjs next.js react-loadable
I have a simple function which loads a script:
const creditCardScript = (
onReadyCB,
onErrorCB,
) => {
let script = document.createElement("script");
script.type = "text/javascript";
script.src = process.CREDIT_CARD_SCRIPT;
document.head.appendChild(script);
script.onload = function() {
...
};
};
export default creditCardScript;
Before I migrated to NextJS, I was importing the script with: import creditCardScript from "./creditCardScript"
.
Sine NextJS renders components server side in Node, care needs to be taken to ensure that any code with a reference to window
(which is browser specific), doesn't get called until componentDidMount
.
NextJS solves this issue by providing dynamic imports (a wrapper around react-loadable) which:
- only load the component when needed,
- provides an option to only load the component on client side
(ssr: false
).
I went ahead and implemented dynamic imports:
const creditCardScript = dynamic(import("./creditCardScript"), { ssr: false });
In componentDidMount
:
componentDidMount = () => {
creditCardScript(
this.onReadyCB,
this.onErrorCB
);
};
But I'm getting this:
Uncaught TypeError: Cannot call a class as a function
I've tried to convert the function to a class and use the constructor to pass in args
, but my code now fails silently.
javascript node.js reactjs next.js react-loadable
javascript node.js reactjs next.js react-loadable
edited Jan 1 at 16:27
Avremel Kaminetzky
asked Jan 1 at 16:22


Avremel KaminetzkyAvremel Kaminetzky
478620
478620
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
As Neal mentioned in the comments, all I need to do is something like this in componentDidMount
:
const { default: creditCardScript } = await import("./creditCardScript");
Link to the official tutorial
1
I am confused, why not just use a regular function?
– Neal
Jan 2 at 15:20
1
And then call it during a client side only lifecycle hook. Dont use the dynamic function -- that is only for react components.
– Neal
Jan 2 at 15:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997032%2fdynamic-imports-nextjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As Neal mentioned in the comments, all I need to do is something like this in componentDidMount
:
const { default: creditCardScript } = await import("./creditCardScript");
Link to the official tutorial
1
I am confused, why not just use a regular function?
– Neal
Jan 2 at 15:20
1
And then call it during a client side only lifecycle hook. Dont use the dynamic function -- that is only for react components.
– Neal
Jan 2 at 15:24
add a comment |
As Neal mentioned in the comments, all I need to do is something like this in componentDidMount
:
const { default: creditCardScript } = await import("./creditCardScript");
Link to the official tutorial
1
I am confused, why not just use a regular function?
– Neal
Jan 2 at 15:20
1
And then call it during a client side only lifecycle hook. Dont use the dynamic function -- that is only for react components.
– Neal
Jan 2 at 15:24
add a comment |
As Neal mentioned in the comments, all I need to do is something like this in componentDidMount
:
const { default: creditCardScript } = await import("./creditCardScript");
Link to the official tutorial
As Neal mentioned in the comments, all I need to do is something like this in componentDidMount
:
const { default: creditCardScript } = await import("./creditCardScript");
Link to the official tutorial
edited Jan 2 at 17:55
answered Jan 1 at 18:38


Avremel KaminetzkyAvremel Kaminetzky
478620
478620
1
I am confused, why not just use a regular function?
– Neal
Jan 2 at 15:20
1
And then call it during a client side only lifecycle hook. Dont use the dynamic function -- that is only for react components.
– Neal
Jan 2 at 15:24
add a comment |
1
I am confused, why not just use a regular function?
– Neal
Jan 2 at 15:20
1
And then call it during a client side only lifecycle hook. Dont use the dynamic function -- that is only for react components.
– Neal
Jan 2 at 15:24
1
1
I am confused, why not just use a regular function?
– Neal
Jan 2 at 15:20
I am confused, why not just use a regular function?
– Neal
Jan 2 at 15:20
1
1
And then call it during a client side only lifecycle hook. Dont use the dynamic function -- that is only for react components.
– Neal
Jan 2 at 15:24
And then call it during a client side only lifecycle hook. Dont use the dynamic function -- that is only for react components.
– Neal
Jan 2 at 15:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997032%2fdynamic-imports-nextjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown