[英]Typescript definition for a stateful React.Component in an interface
What is the correct typing to define for a property that is assigned to a stateful React.Component
inside an interface. 什么是为分配给接口内有状态
React.Component
的属性定义的正确类型。
The TemplateConfiguration
interface has two properties Page
and Form
. TemplateConfiguration
接口具有两个属性Page
和Form
。 Page
's type is a stateless component (Presentational) and Form
's type should be a stateful component. Page
的类型是无状态组件(Presentational),而Form
的类型应该是有状态组件。
Page
works fine, however, the compiler gives me an error two places and I'm not sure what is the proper typing. Page
可以正常工作,但是,编译器在两个地方给我一个错误,我不确定什么是正确的键入。 Please read the comments below. 请阅读下面的评论。
interface ConfigProps {
// ...
}
interface ConfigState {
// ...
}
interface TemplateConfiguration<T> {
Form: React.Component<ConfigProps, ConfigState>;
Page: React.FunctionComponent<T>; // this one works here
}
const myConfig: TemplateConfiguration = {
Page: ({arg1, arg2}) => (<div>...</div>), // and works here
Form: class extends React.Component<ConfigProps, ConfigState> {
// ^^^^ --- Type 'typeof Form' is missing the following properties from type 'Component<ConfigProps, ConfigState, any>': context, setState, forceUpdate, render, and 3 more
render() {
return (<div>Some text here</div>);
}
}
}
class App extends React.Component {
render() {
const { Form, Page } = myConfig;
return (
<div>
<Form /> // JSX element type 'Form' does not have any construct or call signatures
</div>
);
}
}
You have to use React.ComponentClass
instead of React.Component
您必须使用
React.ComponentClass
而不是React.Component
Form: React.Component<ConfigProps, ConfigState>;
should be 应该
Form: React.ComponentClass<ConfigProps, ConfigState>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.