[英]Its return type 'Element | undefined' is not a valid JSX element. Type 'undefined' is not assignable to type 'Element | null'
I checked previous answers for this type of error but most of them regard mapping and I am not sure what I should return in this case.我检查了以前针对此类错误的答案,但大多数答案都与映射有关,我不确定在这种情况下应该返回什么。 My error says that CorrectDataHandler' cannot be used as a JSX component.
我的错误是CorrectDataHandler' 不能用作 JSX 组件。 Its return type 'Element |
它的返回类型 'Element | undefined' is not a valid JSX element.
undefined' 不是有效的 JSX 元素。 Type 'undefined' is not assignable to type 'Element |
类型“undefined”不可分配给类型“Element | null'.
无效的'。
My type and component look as follows:我的类型和组件如下所示:
TYPES:类型:
// ERRORS TYPE
interface SourceType {
pointer: string;
}
interface ErrorsType {
details: string[];
source: SourceType;
title: string;
}
// DATA TYPE
export interface DataType {
content?: string;
filename: string;
format: string;
batch_id?: number;
}
// META TYPE
interface CountryCurrencyType {
iso: string;
name: string;
}
interface MetaType {
country: CountryCurrencyType;
currency: CountryCurrencyType;
local_instrument: string;
payout_requests?: number;
}
// DATA RESPONSE
export interface DataResponse {
data: DataType;
meta?: MetaType;
errors?: ErrorsType;
}
// RESPONSE
export interface Response {
status?: number;
data: DataResponse;
}
COMPONENT:零件:
{!backToUpload && data && ('data' || 'meta') in data && (
<div className="rounded-b-lg bg-white py-10 px-6">
{console.log(data)}
<CorrectDataHandler data={data as DataResponse} />
<button
className="inpay-button mt-10 w-full"
onClick={() => navigateToPayoutBatches(data.data.batch_id)}
>
View batch
</button>
<button
type="submit"
className="inpay-button mt-4 w-full"
onClick={() => {
setBackToUpload(true);
resetForm();
}}
>
Upload another batch
</button>
</div>
)}
The component I render looks like this:我呈现的组件如下所示:
import { DataResponse } from '.';
interface DataProps {
data: DataResponse;
}
const CorrectDataHandler = ({ data }: DataProps) => {
if (data.data && data.meta) {
return (
data && (
<div>
<div>
Format: <span className="font-semibold">CSV</span>
</div>
<div>
Name: <span className="font-semibold">{data.data.filename}</span>
</div>
<div>
Type:{' '}
<span className="font-semibold">{data.meta.local_instrument}</span>
</div>
<div>
Corridor:
<span className="ml-1 font-semibold">
{data.meta.currency.iso} ({data.meta.currency.name}) -{' '}
{data.meta.country.iso} ({data.meta.country.name})
</span>
</div>
<div>
Total: <span>{data.meta.payout_requests}</span>
</div>
</div>
)
);
}
};
Add a default condition when if
fails if
失败时添加默认条件
if (data.data && data.meta) {
return (
....
)
}
return null
You need to explicitly return null
since undefined
is not a valid JSX Element:-您需要显式返回
null
,因为undefined
不是有效的 JSX 元素:-
const CorrectDataHandler = ({ data }: DataProps) => {
if (data.data && data.meta) {
//...
}
return null; // <-- add this return
};
The error message is trying to tell you that your component returns undefined
sometimes , and hence cannot be rendered without fixing it.该错误消息试图告诉您您的组件有时会返回
undefined
,因此如果不修复它就无法呈现。
In your specific case CorrectDataHandler
will return undefined when if (data.data && data.meta)
condition is not met.在您的特定情况下,当不满足
if (data.data && data.meta)
条件时, CorrectDataHandler
将返回 undefined 。
In JS, an implicit undefined
is returned from functions, if there isn't one explicitly provided.在 JS 中,如果没有明确提供,则从函数返回隐式
undefined
。
// this code
function f1() {
if (condition) {
return 'foo';
}
}
// is equivalent to
function f1() {
if (condition) {
return 'foo';
}
return undefined;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.