将react form组件转换为typescript



我有一个反应形式组件,它在JavaScript完美地工作,我想把它转换成TypeScript,但我得到这个错误:Property '_title' does not exist on type 'FormComp'。该组件类似于:

class FormComp extends React.Component<{}> {
constructor(props: any) {
super(props);
this.state = {};
}
handleCreate(event: any) {
event.preventDefault();
var title = this._title.value;
const temp = { title: title };
// This temp object will be send to backend by ajax call 
.
. 
.
}
render() {
return (
<div className="col-md-12">
<input
type="text"
className="col-sm-3 form-control"
placeholder="Title"
ref={(c) => (this._title = c)}
></input>
<button
className="btn btn-primary"
onClick={this.handleCreate.bind(this)}
>
Create
</button>
);
}
}

如何将此组件转换为TypeScript组件?

既然你在使用这个。_title,你必须为TypeScript定义它。

class FormComp extends React.Component<{}> {
_title: string; // or other type
constructor(props: any) {
super(props);
this.state = {};
}
...
}

最新更新