【问题标题】:Is there any way to check if the Component exists before importing that react native有没有办法在导入反应本机之前检查组件是否存在
【发布时间】:2019-03-13 11:53:39
【问题描述】:

我有一个场景,如果该组件的文件存在,我想加载一个组件并渲染它,如果不返回另一个组件。

if('./Shared/MyComponent') exists then 
Import MyCompoent from './Shared/MyCompoent'
Else
Import MyCompoent from './Shared/MyNewCompoent'

有什么办法可以做到这一点

【问题讨论】:

  • 有趣。请问为什么MyComponent 可能不存在?可能还有其他方法可以解决。
  • 我正在创建 React 本机项目,它可以从中生成多个具有相同功能但在登录页面(主页)和不同图标中不同的应用程序。如果自定义页面存在加载或加载基本公共页面,只需尝试通过此实现此目的
  • 有趣。也许您总是可以创建MyComponent 文件,但如果您不使用它,它会导出一个空函数。它还将导出一个 isActive bool,您可以在导入它的组件的 render 方法中使用它,例如import MyComponent, { isActive } from './Shared/MyComponent'
  • 无法使用 React.Lazy 实现这一点。?

标签: reactjs react-native


【解决方案1】:

嗯,这有点 hacky,可能还有更完善的解决方案,但这应该工作(现在无法测试)

try {
 import MyComponent from './component'
} catch (e) {
 import MyComponent from './new-component'
}

【讨论】:

    猜你喜欢
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2021-07-10
    • 1970-01-01
    • 2020-08-05
    相关资源
    最近更新 更多