7
使用TypeScript在React组件中的默认属性值
我不知道如何使用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> ); } } 当我尝试使用这样的组件时: ReactDOM.render(<PageComponent />, document.getElementById("page")); 我收到一条错误消息,指出foo缺少属性。我想使用默认值。我也尝试过static defaultProps = ...在组件内部使用,但是我怀疑它没有任何作用。 src/typescript/main.tsx(8,17): error TS2324: Property 'foo' is missing in type …
151
reactjs
typescript
tsx