我正在尝试将数据与typescript一起引用到 reactJS 中。在执行此操作时,我收到以下错误
Type 'null' is not assignable to type 'HTMLInputElement'
请让我知道这里到底有什么不正确,我使用了 React https://reactjs.org/docs/refs-and-the-dom.html 中的文档, 但我认为我在这里做错了。以下是范围片段
class Results extends React.Component<{}, any> {
private textInput: HTMLInputElement;
.......
constructor(props: any) {
super(props);
this.state = { topics: [], isLoading: false };
this.handleLogin = this.handleLogin.bind(this);
}
componentDidMount() {.....}
handleLogin() {
this.textInput.focus();
var encodedValue = encodeURIComponent(this.textInput.value);
.......
}
render() {
const {topics, isLoading} = this.state;
if (isLoading) {
return <p>Loading...</p>;
}
return (
<div>
<input ref={(thisInput) => {this.textInput = thisInput}} type="text" className="form-control" placeholder="Search"/>
<div className="input-group-btn">
<button className="btn btn-primary" type="button" onClick={this.handleLogin}>
...............
知道我在这里可能缺少什么吗?