在React应用程序中导入条件组件

时间:2018-11-07 19:09:19

标签: reactjs webpack fetch dynamic-import fetch-mock

我正在使用fetch-mock来模拟使用fetch的实际组件。 fetch-mock不支持ie11,仅在支持浏览器的情况下才想导入示例组件。我该如何实现?

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

LoadComponent

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

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

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

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

我不知道我是否完全理解您的问题,但是请尝试一下 const Abc = condition ? require('ComponentA') : require('componentB')