简体   繁体   English

不能用作 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'

I created a react function EventDateTooltip:我创建了一个反应 function EventDateTooltip:

import React from 'react';
import { endOfDay, isToday, isTomorrow, isYesterday } from 'date-fns';
import { useTranslation } from 'react-i18next';
import I18nWrapper from '../i18n-wrapper';
import DateFormater from '../formaters/date-formater';

 const EventDateTooltip = ({ date }: { date: string }) => {
 const { t } = useTranslation();
 const dateTooltip = endOfDay(new Date(date));
  if (isTomorrow(dateTooltip)) return t('tooltip.event.date.tomorrow');
  if (isYesterday(dateTooltip)) return t('tooltip.event.date.yesterday');
  if (isToday(dateTooltip)) return t('tooltip.event.date.today');

 return (
   <>
  <span key={date.toString()}>
    <I18nWrapper translateKey="tooltip.event.date.the" />{' '}
    <DateFormater dateToFomat={date} formater="full-date" />
  </span>
</>
  );
};

export default EventDateTooltip; 

this function return a text and a translated date.这个 function 返回文本和翻译日期。

when I call this function on another component:当我在另一个组件上调用这个 function 时:

 <EventDateTooltip date={props.event.endDate}></EventDateTooltip>;

the call returns the following error message:该调用返回以下错误消息:

Its return type 'string | Element' is not a valid JSX element.
   Type 'string' is not assignable to type 'Element | null'.  TS2786

has anyone got the following message?有没有人收到以下消息?

As stated in this answer , currently TypeScript's React types do not allow you to return a string from a React component and include that in a component tree.本回答所述,目前 TypeScript 的 React 类型不允许您从 React 组件返回字符串并将其包含在组件树中。

You can wrap your strings with fragments to avoid the error:您可以用片段包装字符串以避免错误:

// [...]
  if (isTomorrow(dateTooltip)) return <>t('tooltip.event.date.tomorrow')</>;
  if (isYesterday(dateTooltip)) return <>t('tooltip.event.date.yesterday')</>;
  if (isToday(dateTooltip)) return <>t('tooltip.event.date.today')</>;
// [...]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 组件不能用作 JSX 组件。 它的返回类型'元素 | undefined' 不是有效的 JSX 元素 - Component cannot be used as a JSX component. Its return type 'Element | undefined' is not a valid JSX element 组件不能用作 JSX 组件。 它的返回类型 'Element[]' 不是有效的 JSX 元素 - Component cannot be used as a JSX component. Its return type 'Element[]' is not a valid JSX element “X”不能用作 JSX 组件。 它的返回类型 'Element[]' 不是有效的 JSX 元素 - 'X' cannot be used as a JSX component. Its return type 'Element[]' is not a valid JSX element 它的返回类型 &#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 组件。 它的返回类型“void”不是有效的 JSX element.ts(2786) - cannot be used as a JSX component. Its return type 'void' is not a valid JSX element.ts(2786) &#39;Router&#39; 不能用作 JSX 组件。 它的实例类型“BrowserRouter”不是有效的 JSX 元素 - 'Router' cannot be used as a JSX component. Its instance type 'BrowserRouter' is not a valid JSX element “组件”不能用作 JSX 组件。 它的元素类型&#39;ReactElement<any, any> | Component&lt;{}, any, any&gt;&#39; 不是有效的 JSX 元素 - 'Component' cannot be used as a JSX component. Its element type 'ReactElement<any, any> | Component<{}, any, any>' is not a valid JSX element 'Provider' 不能用作 JSX 组件。 它的实例类型'Provider<anyaction> ' 不是有效的 JSX 元素。 TS2786</anyaction> - 'Provider' cannot be used as a JSX component. Its instance type 'Provider<AnyAction>' is not a valid JSX element. TS2786 RC-dock 库的 &#39;DockLayout&#39; 不能用作 JSX 组件。 它的实例类型“DockLayout”不是有效的 JSX 元素 - RC-dock library's 'DockLayout' cannot be used as a JSX component. Its instance type 'DockLayout' is not a valid JSX element 类型 &#39;JSX.Element&#39; 不可分配给类型 &#39;Element&#39; - Type 'JSX.Element' is not assignable to type 'Element'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM