简体   繁体   English

next.js 从object动态导入组件

[英]next.js dynamic import component from object

I have an object of components that I'll looking to dynamically import using next/dynmaic我有一个 object 的组件,我希望使用next/dynmaic动态导入这些组件

I'm just wondering is it possible.我只是想知道是否有可能。

This is the object in question这是有问题的object

// IconComponents.tsx

import {
    Tick,
    Star
} from 'components/Icons';

export interface Map {
  [name: string]: JSX.Element;
}

const IconObject: Map = {
  tick: <Tick />,
  star: <Star />,
};

export default IconObject;

If I import this in a different file如果我将其导入不同的文件

import dynamic from 'next/dynamic';

const DynamicComponent = dynamic(() => import('./Icons/IconComponents'));

I get the following error我收到以下错误

Argument of type '() => Promise<typeof import("...path")>' is not assignable to parameter of type 'DynamicOptions<{}> | '() => Promise<typeof import("...path")>' 类型的参数不可分配给 'DynamicOptions<{}> | 类型的参数Loader<{}>'.加载程序<{}>'。

Type '() => Promise<typeof import("...path")>' is not assignable to type '() => LoaderComponent<{}>'.类型 '() => Promise<typeof import("...path")>' 不可分配给类型 '() => LoaderComponent<{}>'。

Am I doing sometihng wrong, I need all the component in the object so should I be looping through it?我做错了什么吗,我需要 object 中的所有组件,所以我应该遍历它吗? Any ideas?有任何想法吗?

dynamic(callback)

looks like callback provided in dynamic should return react components看起来dynamic中提供的callback应该返回反应组件

try尝试

const DynamicComponent = dynamic(() => import('./Icons/IconComponents').tick);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM