【问题标题】:exporting default function react native导出默认功能反应原生
【发布时间】:2020-12-14 03:41:38
【问题描述】:

我只是在学习 React Native,在基本示例中,我只有一个扩展 Component 的类,并且是默认导出。

export default class App extends Component {
  // code
}

这很好用,我没问题。

但我发现了其他示例,它们将代码作为函数提供。

export const App = () => {
  // code
}

我看到这个函数被称为 App 并被导出,但是如果我尝试运行它(我使用 expo),我会收到以下消息

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

不确定错误,因为函数 App 已导出(可能错误来自不是默认导出)?

我该如何解决这个问题?

【问题讨论】:

  • 找到了,只需要在文件末尾加上export default App;这行,这样函数App就默认导出了

标签: javascript react-native


【解决方案1】:

如果是默认导出

export default class App extends Component {
  // code
}

像这样导入

import App from "./Apppath"

如果它不是默认导出或命名导出(没有默认关键字)

export const App = () => {
  // code
}

像这样导入

 import {App} from "./Apppath"

docs

【讨论】:

  • App.js 是我的主文件,我应该在哪里导入 App? Expo直接去App.js执行默认类,但本例是一个函数
  • 可能在 index.js 中? @SembeiNorimaki
  • 好的,我解决了。我只需要在文件末尾添加行export default App;
【解决方案2】:

您尚未对 App 组件进行默认导出。在这种情况下,您需要使用以下行 从“..”导入 { App }

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多