我无法弄清楚如何使用Typescript为我的组件设置默认属性值.
这是源代码:
class PageState
{
}
export class PageProps
{
foo: string = "bar";
}
export class PageComponent extends React.Component<PageProps, PageState>
{
public render(): JSX.Element
{
return (
<span>Hello, world</span>
);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用这样的组件时:
ReactDOM.render(<PageComponent />, document.getElementById("page"));
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,说财产foo缺失.我想使用默认值.我也尝试static defaultProps = ...在组件内部使用,但它没有像我怀疑的那样有效.
src/typescript/main.tsx(8,17): error TS2324: Property 'foo' is missing in type 'IntrinsicAttributes & IntrinsicClassAttributes<PageComponent> & PageProps & { children?: ReactEle...'.
Run Code Online (Sandbox Code Playgroud)
我如何使用默认属性值?我公司使用的许多JS组件都依赖于它们而不使用它们不是一种选择.