-
Notifications
You must be signed in to change notification settings - Fork 26.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Imports not working with named exports - Typescript? #22278
Comments
As you are using Typescript have you tried defining an interface for your component props ?
|
That worked, thank you @jvitela! |
@jvitela Thanks!! |
Yes thanks for this. I found in the case of a component with no props, just supplying an empty interface appeased TypeScript. |
if component doesn't have props, just define import dynamic from "next/dynamic";
const MyComponent = dynamic<{}>(() => import("./MyComponent").then(module => module.MyComponent)); |
This works fine |
Thanks a lot man I couldn't find any solution |
I was facing the same problem and investigated it more thoroughly together with @timneutkens's help. Here is what I found: const components = {
// 1
CheckboxFormControl_doesnt_work: dynamic(() =>
import('./blocks/FormBlock/CheckboxFormControl').then((_) => _.CheckboxFormControl)
),
// 2
CheckboxFormControl_works: dynamic(async () => {
const mod = await import('./blocks/FormBlock/CheckboxFormControl');
return mod.CheckboxFormControl;
}),
// 3
CheckboxFormControl_also_works: dynamic( () =>
import('./blocks/FormBlock/CheckboxFormControl').then((_) => _.CheckboxFormControl, () => () => null)
), It seems that using I've ultimately landed on the following pattern: const namedComponent = async <T, N extends keyof T>(modPromise: Promise<T>, exportName: N) => {
const mod = await modPromise;
return mod[exportName];
};
const components = {
CheckboxFormControl: dynamic(() =>
namedComponent(import('./blocks/FormBlock/CheckboxFormControl'), 'CheckboxFormControl')
),
} |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
10.0.5
What version of Node.js are you using?
12.20.1
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
Vercel
Describe the Bug
When using dynamic imports (https://nextjs.org/docs/advanced-features/dynamic-import), everything works fine with default exports. However, when using named exports, I get this error:
Expected Behavior
I expect the component to be imported lazily, without errors.
To Reproduce
The text was updated successfully, but these errors were encountered: