score:228

Accepted answer

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 ­čî╣

score:0

As @basarat said above, when you have something like button.onclick = thisFunction(); you are already calling that function. You may want to just assign that function but not to call it,

so you would write like this button.onclick = thisFunction;.

score:1

You can just set type this way and you will get no errors

export interface IPropsk {
    data?: Array<Items>;
    fetchData?(): (value: string) => void;
}

score:28

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.

score:37

With Functional Components, we use React.MouseEvent and it clears things up...

const clickHandler = () => {
  return (event: React.MouseEvent) => {
    ...do stuff...
    event.preventDefault();
  }
}

Related Query

More Query from same tag