首页 > 解决方案 > 反应应用程序中的条件组件导入

问题描述

fetch-mock用来模拟使用 fetch 的实际组件。fetch-mock 不支持 ie11 并且希望仅在支持浏览器的情况下导入示例组件。我怎样才能做到这一点?

  1. ABCExampleComponentfetch-mock用于ABCComponent 中使用fetch的模拟。
  2. 仅当浏览器受支持时,LoadComponent 才应呈现 ABCExampleComponent。

加载组件

const isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
if(!isIE11){
  import ABCExampleComponent from './ABCExampleComponent';
}

//or
const ABCExampleComponent = !isIE11 ? import('./ABCExampleComponent') : null;

const LoadComponent = ( ) => {
  <ABCExampleComponent />
}

感谢您的建议。

标签: reactjswebpackfetchdynamic-importfetch-mock

解决方案


我不知道我是否完全理解你的问题,但试试这个 const Abc = condition ? require('ComponentA') : require('componentB')


推荐阅读