繁体   English   中英

Typescript:类型“Element[]”不可分配给类型“ReactElement” <any, string | jsxelementconstructor<any> >'</any,>

[英]Typescript: Type 'Element[]' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>>'

我有一个组件声明如下:

const Wrapper = ({children}: {children: ReactElement[]}): ReactElement => {
  return (
    <div className="wrapper">
      {children}
    </div>
  );
};

这个包装器需要一个 ReactElement 数组,例如:

<Wrapper>
  {items.map((item, index) => {
    return (
      <Item />
    )
  })}
</Wrapper>

这工作正常,但是当我想将一个 div 添加到返回的项目列表时,如下所示:

<Wrapper>
   <div /> {/* empty div for the first column */}
  {items.map((item, index) => {
    return (
      <Item />
    )
  })}
</Wrapper>

我收到此错误:

Type 'Element[]' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>>'.ts(2322)

我该如何解决这个问题?

通常 Rect 组件意义上的children项的正确类型是ReactNode (注意它不是数组):

const Wrapper = ({ children }: { children: ReactNode }): ReactElement => {
    return <div className="wrapper">{children}</div>;
};

const Item = () => <div />;

const items = [1, 2, 3];
const x = (
    <Wrapper>
        <div />
        {items.map((item, index) => {
            return <Item />;
        })}
    </Wrapper>
);

游乐场示例

输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,>

[英]Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any)

暂无
暂无

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

相关问题 输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,> “Element”类型的参数不可分配给“ReactElement”类型的参数<any></any> JSX 元素类型 &#39;ReactElement<any> 不是 JSX 元素的构造函数。 类型 &#39;undefined&#39; 不能分配给类型 &#39;Element | 空值&#39; TypeScript - 类型“未定义”不可分配给类型“ReactElement” 输入'{数据:InvitedUser[]; ““: 任何; }' 不可分配给类型 'Element' Typescript:字符串可分配给 {} 类型 typescript:类型&#39;any []&#39;的参数不能分配给&#39;[]&#39;类型的参数.ts(2345) React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 “ReactElement”类型中缺少属性“...”<any, any> ' 但在类型 '{...}' 中是必需的</any,> Typescript 类型“字符串”不可分配给类型
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM