简体   繁体   English

如何使用 defaultProps 为 React 函数组件定义接口而不会抛出缺少类型错误?

[英]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 项目。

编辑 misty-pine-rtdl8

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:由于colorButtonProps类型的唯一属性,您可以使用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>
);

Here is the updated demo .这是更新的演示

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.

相关问题 如何使用箭头 function 组件(rafce)定义 defaultProps? - How to define defaultProps using arrow function component (rafce)? 如何使用条件道具接口定义 React 组件? - How to define React component with conditional props interface? 如何在 React TypeScript 中定义一次类型并将其分配给函数和组件? - How to define a type once and assign it to a function and a component in React TypeScript? 如何在defaultProps React.Component类语法中使用&#39;this&#39;? - How can I use 'this' in defaultProps React.Component class syntax? 在React中的功能组件上设置defaultProps - Setting defaultProps on functional component in React 反应 select - 如何定义组件道具类型 - react select - how to define component props type Angular 组件使用 Javascript 库 function 抛出类型错误:不是 Z86408593C34AF77FDD1Z0DF93 - Angular Component using Javascript Library function throwing Type Error: is Not a Function 未调用回调函数中的React组件并且不引发任何错误 - React Component in Callback Function not called and not throwing any error 作为 prop 传入的 function 的 React 组件抛出 TypeScript 错误 - React component throwing TypeScript error for function passed in as prop 从组件中提取 proptypes、isRequired 和 defaultProps - react extract proptypes, isRequired and defaultProps from component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM