简体   繁体   English

创建一个 Typescript/React/Styled-Components 按钮

[英]Creating a Typescript/React/Styled-Components button

I created a simple styled-components <Button> :我创建了一个简单的样式组件<Button>

interface IButtonProps {
    danger: boolean,
    isLoading: boolean,
    primary: boolean
}

export const Button = styled.button<IButtonProps>`...`

But when trying to use it:但是当尝试使用它时:

<Button onClick={fight}>FIGHT!</Button>

The Button gives the error: Button给出错误:

Type '{ children: string; onClick: () => void; }' is missing the following properties from type 'Pick<Pick<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | "style" | "title" | "key" | "children" | "className" | "color" | "id" | ... 256 more ... | "value"> & { ...; } & ButtonProps, "form" | ... 267 more ... | "primary"> & Partial<...>, "form" | ... 267 more ... | "prim...': danger, isLoading, primaryts(2739)

I tried a few things including extending React.ButtonHTMLAttributes<HTMLButtonElement> with no luck.我尝试了一些方法,包括扩展React.ButtonHTMLAttributes<HTMLButtonElement>没有运气。

You declared danger , isLoading and primary as mandatory props but you didn't declared its.你声明了dangerisLoadingprimary作为强制道具,但你没有声明它。 You should do:你应该做:

<Button primary={false} danger={false} isLoading={false} onClick={fight}>FIGHT!</Button>

Or declare in your interface that props can be optional with question mark like that:或者在您的界面中声明道具可以是可选的,带有问号,如下所示:

interface IButtonProps {
    danger?: boolean,
    isLoading?: boolean,
    primary?: boolean
}

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

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