简体   繁体   English

在子道具上分配 onClick 时出错

[英]error when assigning onClick on a child prop

i there.在这里。

considering these two components:考虑这两个组件:


const Link: React.FC<{
    clicked?: () => {};
    Icon: OverridableComponent<SvgIconTypeMap<{}, 'svg'>>;
    children: string;
}> = ({ Icon, children, clicked }) => {
    return (
        <div className='sidebarLink' onClick={clicked}>
            <Icon />
            <p>{children}</p>
        </div>
    );
};

const Navbar: React.FC<INavbar> = ({ setPage }) => {
    return (
        <nav className='sidebarOptions'>
            <section>
                <Link Icon={GamesIcon}>Better Games</Link>
                <Link Icon={HomeIcon}>Home</Link>
                <Link Icon={StoreIcon}>Store</Link>
                <Link Icon={DashboardIcon} onClick={() => setPage('library')}>
                    Library
                </Link>
                <Link Icon={PeopleAltIcon}>Friends</Link>
                <Link Icon={EmojiEmotionsIcon}>Unreal Engine</Link>
            </section>

            <section className='section2'>
                <p>Downloads</p>
                <p>Settings</p>
                <p>user</p>
            </section>
        </nav>
    );
};

I am getting this error:我收到此错误:

Type '{ children: string;输入'{孩子:字符串; Icon: OverridableComponent<SvgIconTypeMap<{}, "svg">>;图标:OverridableComponent<SvgIconTypeMap<{}, "svg">>; onClick: () => void; onClick: () => void; }' is not assignable to type 'IntrinsicAttributes & { clicked?: (() => {}) | }' 不能分配给类型 'IntrinsicAttributes & { clicked?: (() => {}) | undefined;不明确的; Icon: OverridableComponent<SvgIconTypeMap<{}, "svg">>;图标:OverridableComponent<SvgIconTypeMap<{}, "svg">>; children: string;儿童:字符串; } & { ...; } & { ...; }'. }'。 Property 'onClick' does not exist on type 'IntrinsicAttributes & { clicked?: (() => {}) |属性 'onClick' 不存在于类型 'IntrinsicAttributes & { clicked?: (() => {}) | undefined;不明确的; Icon: OverridableComponent<SvgIconTypeMap<{}, "svg">>;图标:OverridableComponent<SvgIconTypeMap<{}, "svg">>; children: string;儿童:字符串; } & { ...; } & { ...; }'.ts(2322) }'.ts(2322)

what would be the right type for 'clicked' ? 'clicked' 的正确类型是什么?

regards.问候。

Link component expects a prop called clicked , not onClick - rename it. Link组件需要一个名为clicked的道具,而不是onClick - 重命名它。

<Link Icon={DashboardIcon} clicked={() => setPage('library')}>
    Library
</Link>

Secondly, I believe it's a void function, which does not return anything ( setPage looks like a hook).其次,我相信它是一个void函数,它不返回任何东西( setPage看起来像一个钩子)。

clicked?: () => void;

Anyways if setPage does return something, change the void to the stuff that it returns.无论如何,如果setPage确实返回了一些东西,请将void更改为它返回的内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM