简体   繁体   English

如何通过反应动态导入组件? / 动态导入

[英]how to dynamically import components with react ? / Dynamic import

I have only been programming with react for a short time and I don't know if there is a more efficient way to perform this task.我只在很短的时间内使用 react 进行编程,我不知道是否有更有效的方法来执行此任务。 I have a series of files that correspond to some svg files.我有一系列文件对应一些 svg 文件。 An example would be the following.一个例子如下。


export default function CheckIcon(props) {
  return (

    <Icon {...props}>
        options={ CheckIcon }
        />
  );
}

CheckIcon.displayName = 'CheckIcon';

then I import them into a component that I call Icon.js where I make a switch for each of the cases and return the corresponding component.然后我将它们导入到我称为 Icon.js 的组件中,在其中为每个案例进行切换并返回相应的组件。

import React from 'react';
import CloseIcon from './icons/CloseIcon';
import CheckIcon from './icons/CheckIcon';
import SumIcon from './icons/SumIcon';
import SubtractIcon from './icons/SubtractIcon';
import MultiplyIcon from './icons/MultiplyIcon';
import DivideIcon from './icons/DivideIcon';

export default function Icon({ type, ...props }) {
  const getIcon = () => {
    switch (type) {
      case 'close':
        return <CloseIcon {...props} />;
      case 'check':
        return <CheckIcon {...props} />;
      case 'sum':
        return <SumIcon {...props} />;
      case 'subtract':
        return <SubtractIcon {...props} />;
      case 'multiply':
        return <MultiplyIcon {...props} />;
      case 'divide':
        return <DivideIcon {...props} />;
      default:
        break;
    }
  };

  return getIcon();
}


and finally I import the Icon.js file into the component where I am going to display my html最后,我将 Icon.js 文件导入到要显示 html 的组件中

  <div>

    {() => {
      return (
          <Icon type="close" />
          <Icon type="check" />
          <Icon type="sum" />
          <Icon type="subtract" />
          <Icon type="multiply" />
          <Icon type="divide" />
      );
    }}
  </div>

My question is, is there a more efficient way to dynamically import into my Icon.js component the different components that correspond to each of the seg?我的问题是,是否有更有效的方法将与每个 seg 对应的不同组件动态导入到我的 Icon.js 组件中?

thank you all for your time and help in advance.提前感谢大家的时间和帮助。

I think you could alternatively do it by using type as the icon name the use require to dynamically load your icon as following:我认为您也可以使用type作为require名称来动态加载您的图标,如下所示:

// Icon.js
import React from 'react';

export default function Icon({ type, ...props }) {
  const TheIcon = require(`./icons/${type}`).default;

  return <TheIcon {...props} />
}

// Then use it
import Icon from './Icon';

<Icon type="CloseIcon" />

You can do this with Dynamic Imports .您可以使用动态导入来做到这一点。

But the real answer is that you're probably using a bundler for your code, and his will depend on what bundler you are using.但真正的答案是您可能正在为您的代码使用捆绑器,而他将取决于您使用的捆绑器。 Some bundlers have different syntax.一些捆绑器有不同的语法。 Webpack will recognise import() . Webpack将识别import()

But the real real answer is that if you're using a bundler, don't try to dynamically import code.但真正的答案是,如果您使用的是捆绑程序,请不要尝试动态导入代码。 It's an early optimization that will cause you more problems than it is worth.这是一个早期的优化,会给你带来更多的问题而不是它的价值。 Let the bundler do its thing.让捆绑器做它的事情。 Webpack does code splitting and tree shaking and as your code base gets larger, it will be easier to just let Webpack apply the optimizations. Webpack 进行代码拆分树摇动,随着您的代码库变得更大,让 Webpack 应用优化会更容易。

See also the Dynamic Imports and Lazy Loading sections of the Webpack documentation (if you're using Webpack).另请参阅 Webpack 文档的 动态导入延迟加载部分(如果您使用的是 Webpack)。

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

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