类型“void”不能分配给类型'((event: MouseEvent<HTMLInputElement>) => void) |未定义'修复更多
2022-08-30 04:58:05
import * as React from "react";
import "./App.css";
import PageTwo from "./components/PageTwo";
export interface IPropsk {
data?: Array<Items>;
fetchData?(value: string): void;
}
export interface IState {
isLoaded: boolean;
hits: Array<Items>;
value: string;
}
class App extends React.Component<IPropsk, IState> {
constructor(props: IPropsk) {
super(props);
this.state = {
isLoaded: false,
hits: [],
value: ""
this.handleChange = this.handleChange.bind(this);
}
fetchData = val => {
alert(val);
};
handleChange(event) {
this.setState({ value: event.target.value });
}
render() {
return (
<div>
<div>
<input type="text" value={this.state.value} onChange= {this.handleChange}
<input type="button" onClick={this.fetchData("dfd")} value="Search" />
</div>
</div>
);
}
}
export default App;
在上面的代码示例中,我尝试通过单击带有paremeter的按钮来调用方法(fetchData)。但是我从下面的行中给出了一个错误
<input type="button" onClick={this.fetchData("dfd")} value="Search" />
错误是
类型 'void' 不能分配给类型 '((event: MouseEvent) => void) |未定义'。