javascript - React 组件 - 元素 | 不明确的
问题描述
将 Ionic Modal 实现为 React 组件我收到以下消息:
类型 '({ onClose, tipo }: PropsWithChildren<{ onClose: any; tipo: number; }>) => 元素 | undefined' 不可分配给类型 'FC<{ onClose: any; 提示:数字;}>'。输入'元素 | undefined' 不可分配给类型 'ReactElement<any, any> | 无效的'。类型 'undefined' 不可分配给类型 'ReactElement<any, any> | 空'.ts(2322)
我没有看到问题,因为组件返回元素。所以这不是函数返回问题。
这是有问题的代码。
const MyModal: React.FC<{ onClose: any; tipo: number }> = ({
onClose,
tipo
}) => {
if (tipo === 0) {
return (
<>
<IonHeader>
<IonToolbar>
<IonIcon
icon={arrowBack}
onClick={() => onClose(null)}
slot="start"
id="flecha-volver"
>
{" "}
</IonIcon>
</IonToolbar>
</IonHeader>
<IonContent>
<div id="contenedor-central">
<IonGrid>
<IonRow>
<strong id="elementos">Usuario no registrado</strong>
</IonRow>
<IonRow>
<IonButton href="/registro" id="elementos">
Registrarse
</IonButton>
</IonRow>
</IonGrid>
</div>
</IonContent>
</>
);
}
if (tipo === 1) {
return (
<>
<IonHeader>
<IonToolbar>
<IonIcon
icon={arrowBack}
onClick={() => onClose(null)}
slot="start"
id="flecha-volver"
></IonIcon>
</IonToolbar>
</IonHeader>
<IonContent>
<div id="contenedor-central">
<strong>Usuario no registrado</strong>
</div>
<IonButton href="/registro">Registrarse</IonButton>
<IonItem>Click List Item To Return Selected Value</IonItem>
</IonContent>
</>
);
}
if (tipo === 2) {
return (
<>
<IonHeader>
<IonToolbar>
<IonIcon
icon={arrowBack}
onClick={() => onClose(null)}
slot="start"
id="flecha-volver"
>
{" "}
</IonIcon>
</IonToolbar>
</IonHeader>
<IonContent>
<div id="contenedor-central">
<IonGrid>
<IonRow>
<strong id="elementos">Categorías</strong>
</IonRow>
<IonRow>
<IonButton href="/registro" id="elementos">
Registrarse
</IonButton>
</IonRow>
</IonGrid>
</div>
</IonContent>
</>
);
}
};
解决方案
如果错字只有三个可能的值,请像这样定义
type Typo = 1 | 2 | 3;
const MyModal: React.FC<{onClose: any; tipo: Typo;}>
你的错误必须消失:)
推荐阅读
- python - 根据值拆分列并创建新的数据框
- discord.js - 如何修复该 quick.db 不会产生负面输出
- kubernetes - 使用 terraform 在活动集群中部署 Vault
- swiftui - 在 SwiftUI 中实现登录导航
- php - 用于 php Web 服务的斜杠
- sql - 在什么情况下如何使用 IS NOT NULL?
- python - Exchangelib:按会话 ID 过滤电子邮件
- logstash-grok - 如何从日志的请求行中过滤值
- visual-studio-code - 我需要 Visual Studio Code Insiders 来安装 jupyter Notebook 扩展吗?
- php - AddSelect 在 searchQueryBuilder 使用条件