Type 'void' is not assignable to type '((event: MouseEvent<HTMLInputElement>) => void) | undefined'
In your code this.fetchData("dfd")
you are calling the function. The function returns void
. void
is not assingable to onClick
which expects a function.
Fix
Create a new function that calls fetchData e.g. onClick={() => this.fetchData("dfd")}
.
More
This is a very common error prevented by TypeScript
You could also do something like
fetchData = (val: string) => (event: any) => {
alert(val);
};
Alternatively, you can set a type for your event
, such as React.MouseEvent
. You can read more about it here.
With Functional Components, we use React.MouseEvent
and it clears things up...
const clickHandler = () => {
return (event: React.MouseEvent) => {
...do stuff...
event.preventDefault();
}
}