【问题标题】:Getting Error when try to add external font in React Native App尝试在 React Native App 中添加外部字体时出错
【发布时间】:2021-06-09 15:32:38
【问题描述】:
在我的第一个应用程序中尝试添加外部下载字体时,我是 react native 的新手并面临问题。系统给出以下错误
-
错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。
-
检查LandingPage的渲染方法。
并附上我编写的代码图像。请提出解决方案并提前致谢
【问题讨论】:
标签:
reactjs
react-native
nativescript
custom-font
【解决方案1】:
出现此错误的原因:Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 是因为您使用的组件/函数/类未正确导入。
有区别
import { MyComponent } from './path-to-component/MyComponent.js'
和
import MyComponent from './path-to-component/MyComponent.js'
如果您使用 export default class MyComponent 从另一个文件中导出了一个组件,那么您应该使用此语句 import MyComponent from './path-to-file/MyComponent.js' 导入它
但如果您使用此语句export class MyComponent 导出组件,那么您应该使用此语句import { MyComponent } from './path-to-file/MyComponent.js' 导入它
我的建议是检查您导出的class、function 和component,以及导入它的其他文件。