reactjs - 反应应用程序中的条件组件导入
问题描述
我fetch-mock
用来模拟使用 fetch 的实际组件。fetch-mock 不支持 ie11 并且希望仅在支持浏览器的情况下导入示例组件。我怎样才能做到这一点?
- ABCExampleComponent
fetch-mock
用于ABCComponent 中使用fetch
的模拟。 - 仅当浏览器受支持时,LoadComponent 才应呈现 ABCExampleComponent。
加载组件
const isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
if(!isIE11){
import ABCExampleComponent from './ABCExampleComponent';
}
//or
const ABCExampleComponent = !isIE11 ? import('./ABCExampleComponent') : null;
const LoadComponent = ( ) => {
<ABCExampleComponent />
}
感谢您的建议。
解决方案
我不知道我是否完全理解你的问题,但试试这个
const Abc = condition ? require('ComponentA') : require('componentB')
推荐阅读
- javascript - 如何将动态对象从循环中推入数组
- javascript - 使用方法 setPaths 的问题(Leaflet PolylineDecorator)
- python - Directory.GetFiles("path/to/dir", "*.*", SearchOption.AllDirectories) 但在 Python 中
- android-arrayadapter - Android ArrayAdapter 使用 ANSI 转义序列颜色为文本着色
- r - 在闪亮的服务器中过滤数据集时过滤器中的使用方法错误
- node.js - 在 Nodejs 中测试时在 Rails 中找到相同的 FactoryBot 库
- javascript - How to set Image source as Input File using JavaScript?
- flutter - 我们是否应该对模型使用“延迟”来修复安全空错误
- googletest - 在特定情况下宽恕 googletest 失败
- rxjs - 多次使用 forkJoin