“在 React 中使用 Typescript,预期 1 个参数,但实际得到 0 个参数”。

huangapple go评论63阅读模式
英文:

"Expected 1 arguments, but got 0." for onClick event in React with Typescript

问题

接口 IProps {
    handleClick: React.Dispatch<React.SetStateAction<boolean>>;
    icon?: JSX.Element;
}

const NavLinks: React.FC<IProps> = ({ handleClick }) => (
    <div className="sidebar_navlinks">
        {sidebar_links.map((link) => (
            <NavLink key={link.name} to={link.to} onClick={() => handleClick && handleClick()}>
                <div className="link">
                    {link.icon && <link.icon className="icon" />}
                    {link.name}
                </div>
            </NavLink>
        ))}
    </div>
)

<NavLinks handleClick={() => setMenuState(false)} />
英文:

I have a handleClick function that I'm trying to pass as a prop for my onClick event within a component. This event just takes a setState function. I set an interface to type this as "handleClick: React.Dispatch<React.SetStateAction<boolean>>;" as nothing else worked and would always give errors, so I assumed all was well, until I went ahead with writing the onClick event into the component declaration, when the error in the title appeared.

Here's the relevant code:

interface IProps {
    handleClick: React.Dispatch&lt;React.SetStateAction&lt;boolean&gt;&gt;;
    icon?: JSX.Element;
}

const NavLinks: React.FC&lt;IProps&gt; = ({ handleClick }) =&gt; (
    &lt;div className=&quot;sidebar_navlinks&quot;&gt;
        {sidebar_links.map((link) =&gt; (
            &lt;NavLink key={link.name}  to={link.to} onClick={() =&gt; handleClick &amp;&amp; handleClick()}&gt;
                &lt;div className=&quot;link&quot;&gt;
                    &lt;link.icon className=&quot;icon&quot; /&gt;
                    {link.name}
                &lt;/div&gt;
            &lt;/NavLink&gt;
        ))}
    &lt;/div&gt;
)

And then with that component I just do something like

&lt;NavLinks handleClick={() =&gt; setMenuState(false)} /&gt;

How can I best type this so it stops giving the error in the title? I'm not clear why it would expect there's a value when I'm typed it to be something that sets state?

I see stuff online that, more often than not, is assuming the onClick is going to apply to an HTML button element, but I'm just using this to click on react-icons, so I'm even more lost.

答案1

得分: 1

handleClick 应该是 () => void 类型,因为 setMenuState 包装在一个函数中。

interface IProps {
    handleClick: () => void;
    icon?: JSX.Element;
}

如果您直接传递 setMenuState,则它可以被类型化为一个设置状态的函数。

英文:

handleClick should be of type () =&gt; void since setMenuState is wrapped in a function.

interface IProps {
    handleClick: () =&gt; void;
    icon?: JSX.Element;
}

If you passed setMenuState directly like:

&lt;NavLinks handleClick={setMenuState} /&gt;

then it can be typed as a setState function

huangapple
  • 本文由 发表于 2023年2月19日 16:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定