简体   繁体   English

TS2345:“ReactNode”类型的参数不可分配给“ReactElement”类型的参数

[英]TS2345: Argument of type 'ReactNode' is not assignable to parameter of type 'ReactElement'

TS2345: Argument of type 'ReactNode' is not assignable to parameter of type 'ReactElement<any, string | TS2345:“ReactNode”类型的参数不可分配给“ReactElement<any, string | 类型的参数” ((props: any) => ReactElement<any, any> | null) | ((props: any) => ReactElement<any, any> | null) | (new (props: any) => Component<any, any, any>)> | (new (props: any) => 组件<any, any, any>)> | ReactElement<any, string |... 1 more... | ReactElement<any, string |... 1 更多... | (new (props: any) => Component<...>)>[]'. (new (props: any) => 组件<...>)>[]'。 Type 'undefined' is not assignable to type 'ReactElement<any, string |类型 'undefined' 不可分配给类型 'ReactElement<any, string | ((props: any) => ReactElement<any, any> | null) | ((props: any) => ReactElement<any, any> | null) | (new (props: any) => Component<any, any, any>)> | (new (props: any) => 组件<any, any, any>)> | ReactElement<any, string |... 1 more... | ReactElement<any, string |... 1 更多... | (new (props: any) => Component<...>)>[]'. (new (props: any) => 组件<...>)>[]'。

Image of the error错误图片

Here's my code:这是我的代码:

import React, {forwardRef, ReactNode} from 'react';

import {mergeClasses} from '../../utils';
import {ArrowLeft} from '../../icons';

import {BreadcrumbsProps} from './declarations/Breadcrumbs.types';

import defaultClasses from './styles/Breadcrumbs.module.css'

export const Breadcrumbs = forwardRef<HTMLLabelElement, BreadcrumbsProps>(
    ({
        separator="/",
        children,
        back,
        classNames,
        onClick,
     }) => {
        const styles = mergeClasses(defaultClasses, classNames);

        const Back = (
            <button className={styles.back} type='button' onClick={onClick}>
                <ArrowLeft/>
                <span>Back</span>
            </button>
        )

        const Separator = <span className={styles.separator}>{separator}</span>;

        const getValidChild = (itemToRender: ReactNode, delta: number, index: number) => {
            const SeparatorWithKey = React.cloneElement(Separator, { key: `${index}_separator` });

            return index === 0 ? [itemToRender] : [SeparatorWithKey, itemToRender];
        };

        const getItemsByChildren = () => {
            const delta = React.Children.toArray(children).length;
            return (
                React.Children.map(children, (child: React.ReactElement, index) => {
                return (
                    child &&
                    getValidChild(
                        React.cloneElement(child),
                        delta,
                        index
                    )
                );
            }));
        };

        return (
            <div className={styles.wrapper}>
                {back && Back}
                <ul className={styles.breadcrumbs}>
                    {getItemsByChildren()}
                </ul>
            </div>
        );
    },
);

You've declared the type of child wrong here:您在这里声明了child的类型错误:

React.Children.map(children, (child: React.ReactElement, index) => {

children is of type React.ReactNode . childrenReact.ReactNode类型。 React.Element is something entirely different. React.Element完全不同的东西。

You don't need to type this argument at all, React.Children.map already can infer the correct type here.你根本不需要输入这个参数, React.Children.map已经可以在这里推断出正确的类型。 Just this works just fine:只是这工作得很好:

React.Children.map(children, (child, index) => {

Playground 操场

暂无
暂无

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

相关问题 如何修复:ReactDOM.render()-TS2345:类型“()=&gt;元素”的参数不能分配给类型“ ReactElement”的参数 - How to fix: ReactDOM.render() - TS2345: Argument of type '() => Element' is not assignable to parameter of type 'ReactElement' TS2345:“字符串”类型的参数 | undefined&#39; 不能分配给类型为 &#39;string&#39; 的参数。 “未定义”类型不能分配给“字符串”类型 - TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string' React Router (v6) useNavigate TypeScript 错误:“TS2345:‘number’类型的参数不可分配给‘To’类型的参数” - React Router (v6) useNavigate TypeScript error: "TS2345: Argument of type 'number' is not assignable to parameter of type 'To'" 了解 TS2345:“'typeof MyComponent' 类型的参数不可分配给类型参数” - Understanding TS2345: "Argument of type 'typeof MyComponent' is not assignable to parameter of type" TS2345:“日期”类型的参数 | null' 不可分配给类型为“string |”的参数日期 | 不明确的' - TS2345: Argument of type 'Date | null' is not assignable to parameter of type 'string | Date | undefined' react-navigation 5.x:TS2345:“...”类型的参数不可分配给“...”类型的参数 - react-navigation 5.x: TS2345: Argument of type ' ... ' is not assignable to parameter of type '... ' 人员类型的 TypeScript 错误 (TS2345) 参数 | undefined 不能分配给 Person 类型的参数 - TypeScript Error (TS2345) argument of type Person | undefined is not assignable to paramater of type Person “any”类型的参数不可分配给“never”类型的参数。ts(2345) - Argument of type 'any' is not assignable to parameter of type 'never'.ts(2345) TS2345 void 类型的参数不可分配 | 反应 useState 钩子 + promise - TS2345 Argument of type void not assignable | React useState hook + promise “元素”类型的参数不可分配给 ReactElement 类型的参数 - Argument of type 'Element' is not assignable to parameter of type ReactElement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM