javascript - 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:
问题描述
当我导出以下组件时,为什么会出现错误Expected a string (for built-in components) or a class/function (for composite components) but got: <Fragment />. Did you accidentally export a JSX literal instead of a component?
和Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object
?
const MyComponent = (
<>
<h1>Hello</h1>
<div>Lorem Ipsum</div>
</>
)
export default MyComponent
这个问题与链接的问题不同。它与某人可能会发现和谷歌搜索的特定错误有关。
解决方案
解决办法是把组件改成函数:
const MyComponent = () => (
<>
<h1>Hello</h1>
<div>Lorem Ipsum</div>
</>
)
export default MyComponent
如果您使用的是打字稿,则返回类型为React.ReactElement
推荐阅读
- python - 将列表值转换为数据框
- pandas - 对均值 pandas 的 T 检验
- javascript - 在 Angular 中的单选按钮上选择所有实现
- javascript - 为几个输入 Vue.js 计算出不同的结果
- python - 如何在不更改主分支的情况下更改本地 git 存储库的功能分支?
- scala - java.lang.NoSuchMethodError:com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
- php - 参数中的 URL 重定向到 403?
- python - 如何在python中为不同的条形图组赋予不同的颜色?
- java - 为什么从其他类调用 JPanel 会给出 NullPointerException 错误?
- javascript - 如何将物体的道具从一个位置转移到另一个位置?