【问题标题】:Conditional component import in react application反应应用程序中的条件组件导入
【发布时间】:2018-11-07 19:09:18
【问题描述】:

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

  1. ABCExampleComponent 使用fetch-mock 模拟ABCComponent 中使用的fetch
  2. Lo​​adComponent 仅在浏览器受支持时才应呈现 ABCExampleComponent。

加载组件

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

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

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

感谢您的建议。

【问题讨论】:

  • 你使用的是什么版本的 React?可以选择 v16.6 吗?

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


【解决方案1】:

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

【讨论】:

  • 我猜 webpacker 在我进行条件检查之前会先查找所有导入/需要,所以这不会起作用。不过谢谢你的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 2020-03-20
  • 2016-07-20
  • 1970-01-01
  • 2017-11-01
  • 2023-03-15
  • 2021-03-06
相关资源
最近更新 更多