reactjs - React - 不能用作 JSX 组件。它的返回类型 'void' 不是有效的 JSX 元素
问题描述
这是一个超级简单的应用程序,它应该只显示下面带有测试的输入以及输入字段中输入的内容的输出。
我在输入组件上遇到错误
'CustomInput' cannot be used as a JSX component.
Its return type 'void' is not a valid JSX element.
谁能看到我为什么会收到此错误
解决方案
返回后需要加括号:
const CustomInput = ({children, value, onChange}: CustomInputProps) => {
return (
<div>
<label htmlFor="search">{children}</label>
<input id="search" type="text" value={value} onChange={onChange} />
</div>
)
}
https://stackblitz.com/edit/react-ts-pb6jpc?embed=1&file=index.tsx
如果你正在写作
const CustomInput = ({children, value, onChange}: CustomInputProps) => {
return
<div>
<label htmlFor="search">{children}</label>
<input id="search" type="text" value={value} onChange={onChange} />
</div>
}
这被转换为
const CustomInput = ({children, value, onChange}: CustomInputProps) => {
return;
<div>
<label htmlFor="search">{children}</label>
<input id="search" type="text" value={value} onChange={onChange} />
</div>
}
所以你的函数基本上返回undefined
并被解释为
const CustomInput = ({children, value, onChange}: CustomInputProps) => {
return undefined;
// nothing after return counts
}
推荐阅读
- php - 如果 7 列匹配,则检查 2 个表中的列并在第 3 个表中保存 2 列
- c++ - 在没有组合/装饰器的父类之前初始化子类
- phpmyadmin - phpmyadmin 登录页面不显示
- c# - 为什么部署在 Azure 上的 Web 应用程序的日期格式会发生变化?(ASP .NET Core)
- swift - 将文本药丸换成新行
- python - Flask DB 迁移以使用绑定支持多个数据库
- c# - 如何用我所有应用程序的随机类替换实现中的 dotnet core System.Random?
- python-3.x - PyMongo $set/update 中的错字导致 TypeError: unhashable type: 'dict'
- linux - Docker 绑定源路径不存在,即使它存在
- android - 在适用于 Android 的 AWS Cognito 中记住设备