我无法弄清楚如何使用我的打字原稿设置组件的默认属性值作出反应的组成部分。默认属性数值的使用打字稿
这是源代码:
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>
);
}
}
当我尝试使用这样的组件:
ReactDOM.render(<PageComponent />, document.getElementById("page"));
我得到一个错误说属性foo
丢失。我想使用默认值。我也尝试在组件内部使用static defaultProps = ...
,但它没有影响,因为我怀疑。
src/typescript/main.tsx(8,17): error TS2324: Property 'foo' is missing in type 'IntrinsicAttributes & IntrinsicClassAttributes<PageComponent> & PageProps & { children?: ReactEle...'.
如何使用默认属性值?我公司使用的许多JS组件都依赖于它们,而不使用它们不是一种选择。
'static defaultProps' is correct。你可以发布该代码吗? – Aaron