[英]Typescript React/Nextjs styled-components - passing function as prop to component throws error on missing type
I'm new to typescript and struggling with a typescript error while passing down the function "toggle" as a prop to the styled-component "MobileIcon".我是打字稿的新手,在将函数“toggle”作为道具传递给样式组件“MobileIcon”时遇到打字稿错误。 I have set the function type for "toggle" in the interface IProps and added it to the styled-component.
我已经在界面 IProps 中为“toggle”设置了函数类型并将其添加到样式组件中。
Layout.tsx:布局.tsx:
import { FunctionComponent as FC, ReactNode, useState } from "react";
import Toolbar from "src/components/Toolbar";
interface IProps {
main: ReactNode;
}
const Layout: FC<IProps> = ({ main }) => {
const [isOpen, setIsOpen] = useState<boolean>(false);
const toggle = () => {
setIsOpen(!isOpen);
console.log("toggle ok");
};
return (
<div>
<nav>
<Toolbar isOpen={isOpen} toggle={toggle} />
</nav>
<main>{main}</main>
</div>
);
};
export default Layout;
Toolbar.tsx:工具栏.tsx:
import { FunctionComponent as FC } from "react";
import styled from "styled-components";
import Link from "next/link";
// Components
import Brand from "./Brand";
interface IProps {
isOpen: boolean;
toggle: () => void;
}
const Toolbar: FC<IProps> = ({ isOpen, toggle }) => {
return (
<Container>
<Link href="/">
<a>
<Brand />
</a>
</Link>
<MobileIcon isOpen={isOpen} onClick={toggle}>...</MobileIcon>
</Container>
);
};
export default Toolbar;
styled-component:样式组件:
const MobileIcon = styled.div<IProps>`
...
`
Error:错误:
No overload matches this call.
Overload 1 of 2, '(props: Omit<Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<...>> & { ...; } & IProps, never> & Partial<...>, "theme"> & { ...; } & { ...; }): ReactElement<...>', gave the following error.
Property 'toggle' is missing in type '{ children: Element[]; isOpen: boolean; onClick: () => void; }' but required in type 'Omit<Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<HTMLDivElement>> & { ...; } & IProps, never> & Partial<...>, "theme">'.
Overload 2 of 2, '(props: StyledComponentPropsWithAs<"div", any, IProps, never, "div", "div">): ReactElement<StyledComponentPropsWithAs<"div", any, IProps, never, "div", "div">, string | JSXElementConstructor<...>>', gave the following error.
Property 'toggle' is missing in type '{ children: Element[]; isOpen: boolean; onClick: () => void; }' but required in type 'Omit<Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<HTMLDivElement>> & { ...; } & IProps, never> & Partial<...>, "theme">'.
Any help would be great.任何帮助都会很棒。
On your JSX, you use your MobileIcon as this:在你的 JSX 上,你使用你的 MobileIcon:
<MobileIcon isOpen={isOpen} onClick={toggle}>...</MobileIcon>
So the props of MobileIcon here are isOpen and onClick.所以这里MobileIcon的props是isOpen和onClick。 You don't have a toggle prop.
你没有切换道具。
Your MobileIcon props interface is then:您的 MobileIcon 道具界面是:
interface MobileIconProps = {
isOpen: boolean;
onClick: () => void;
}
And can be used as:并且可以用作:
const MobileIcon = styled.div<MobileIconProps>
Your IProps
specifies a toggle
prop.您的
IProps
指定了一个toggle
道具。 Therefore your MobileIcon
component also expects that prop, yet you don't specify it in your Toolbar
component.因此,您的
MobileIcon
组件也需要该道具,但您没有在Toolbar
组件中指定它。
Perhaps you didn't want your MobileIcon
to use the props specified by IProps
, but just {}
(no props) instead.也许你不希望你的
MobileIcon
使用由指定的道具IProps
,只是{}
无道具)来代替。 After all, a div
doesn't have isOpen
or toggle
.毕竟,
div
没有isOpen
或toggle
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.