[英]How to define an interface for React function component with defaultProps without throwing missing type error?
I've been searching through many forums and articles and did not find any successful way to define an interface for a function component in React that has required properties which are defined in defaultProps without throwing a "property is missing" Typescript error.我一直在搜索许多论坛和文章,但没有找到任何成功的方法来为 React 中的函数组件定义接口,该函数组件具有在 defaultProps 中定义的所需属性,而不会抛出“缺少属性”的 Typescript 错误。
I've tried to set a default value directly in props but it doesn't work either.我试图直接在 props 中设置一个默认值,但它也不起作用。 Is there any way or it's unresolved issue in React and Typescript?在 React 和 Typescript 中,有什么方法或未解决的问题吗?
To see this problem in action, I've provided CodeSandbox project.为了看到这个问题的实际效果,我提供了 CodeSandbox 项目。
type ButtonProps = {
color: "white" | "green";
};
const Button: FunctionComponent<ButtonProps> = ({
// First way to define default value
color = "green",
children
}) => <ButtonContainer>{children}</ButtonContainer>;
// Second way to define default value
Button.defaultProps = {
color: "white"
} as Partial<ButtonProps>;
const ButtonContainer = styled.button<ButtonProps>`
background-color: ${(props: ButtonProps) => props.color};
`;
const App: FunctionComponent = () => (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
{/* Here is an error */}
<Button>hello world</Button>
</div>
);
Since color
is the only property on the ButtonProps
type, you can use Partial , which will set the properties of the type as optional:由于color
是ButtonProps
类型的唯一属性,您可以使用Partial ,它将类型的属性设置为可选:
const Button: FunctionComponent<Partial<ButtonProps>> = ({
// First way to define default value
color = "green",
children
}) => <ButtonContainer>{children}</ButtonContainer>;
Here is a working demo .这是一个工作演示。
Edit :编辑:
Following up on your comment, this is what I would recommend.跟进您的评论,这就是我的建议。 We can remove the defaultProps
, and use ButtonProps
to define the typings for ButtonContainer
.我们可以去除defaultProps
,并使用ButtonProps
定义为分型ButtonContainer
。 On the Button
component, you may simply spread the remaining props into the child ButtonContainer
.在Button
组件上,您可以简单地将剩余的道具传播到子ButtonContainer
。
type ButtonProps = {
color: "white" | "green";
fontSize: string;
};
const Button: FunctionComponent<Partial<ButtonProps>> = ({
// First way to define default value
children,
...styleProps
}) => <ButtonContainer {...styleProps}>{children}</ButtonContainer>;
const ButtonContainer = styled.button<ButtonProps>`
background-color: ${(props: ButtonProps) => props.color};
font-size: ${(props: ButtonProps) => props.fontSize};
`;
const App: FunctionComponent = () => (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<Button color='green'>hello world</Button>
</div>
);
Change your ButtonProps like this像这样改变你的 ButtonProps
type ButtonProps = {
color?: "white" | "green";
};
Making the color props optional.使颜色道具可选。 And then you can initialize it with a default value, just like you are doing.然后你可以用默认值初始化它,就像你在做的那样。
const Button: FunctionComponent<ButtonProps> = ({
// First way to define default value
color = "green",
children
}) => <ButtonContainer>{children}</ButtonContainer>;
Hope it helps!希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.