简体   繁体   English

它的返回类型 'Element | undefined' 不是有效的 JSX 元素。 类型“undefined”不可分配给类型“Element | 无效的'

[英]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.

相关问题 它的返回类型 &#39;void | Element&#39; 不是有效的 JSX 元素。 类型 &#39;void&#39; 不能分配给类型 &#39;Element | 空值&#39; - Its return type 'void | Element' is not a valid JSX element. Type 'void' is not assignable to type 'Element | null' 不能用作 JSX 组件 它的返回类型 string | 元素不是有效的 JSX 元素类型“string”不可分配给类型“Element | 无效的' - cannot be used as a JSX component Its return type string | Element is not a valid JSX element Type 'string' is not assignable to type 'Element | null' JSX 元素类型 &#39;ReactElement<any> 不是 JSX 元素的构造函数。 类型 &#39;undefined&#39; 不能分配给类型 &#39;Element | 空值&#39; - JSX element type 'ReactElement<any> is not a constructor function for JSX elements. Type 'undefined' is not assignable to type 'Element | null' 组件不能用作 JSX 组件。 它的返回类型'元素 | undefined' 不是有效的 JSX 元素 - Component cannot be used as a JSX component. Its return type 'Element | undefined' is not a valid JSX element 类型“undefined”不可分配给类型“Element | 无效的' - Type 'undefined' is not assignable to type 'Element | null' 它的返回类型 'void' 不是有效的 JSX 元素 - Its return type 'void' is not a valid JSX element 具有 function 子项的组件产生错误“它的返回类型‘ReactNode’不是有效的 JSX 元素。” - Component with function child yields error "Its return type 'ReactNode' is not a valid JSX element." 输入 '0 | 元素 | undefined' 不可分配给类型 'Element' - Type '0 | Element | undefined' is not assignable to type 'Element' 类型 &#39;JSX.Element&#39; 不可分配给类型 &#39;Element&#39; - Type 'JSX.Element' is not assignable to type 'Element' 它的实例类型'Provider<anyaction> ' 不是有效的 JSX 元素。 render() 返回的类型</anyaction> - Its instance type 'Provider<AnyAction>' is not a valid JSX element. The types returned by render()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM