close

[Fixed] Type ‘void’ is not assignable to type ((event: MouseEvent) => void) | undefined

Today We are Going To Solve Type ‘void’ is not assignable to type ((event: MouseEvent) => void) | undefined in Javascript. Here we will Discuss All Possible Solutions and How this error Occurs So let’s get started with this Article.

How to Fix Type ‘void’ is not assignable to type ((event: MouseEvent) => void) | undefined Error?

  1. How to Fix Type 'void' is not assignable to type ((event: MouseEvent) => void) | undefined Error?

    To Fix Type 'void' is not assignable to type ((event: MouseEvent) => void) | undefined Error just Create a new function. Here to solve this error you have to create a new function that calls fetchData. You can better understand this by giving the below example: onClick={() => this.fetchData("dfd")}

  2. Type 'void' is not assignable to type ((event: MouseEvent) => void) | undefined

    To Fix Type 'void' is not assignable to type ((event: MouseEvent) => void) | undefined Error just Use React.MouseEvent. To solve the error you have to just use React.MouseEvent. You can use it in the way given in the below code. So just try this. const clickHandler = () => { return (event: React.MouseEvent) => { ...do stuff... event.preventDefault(); } }

Solution 1 : Create a new function

Here to solve this error you have to create a new function that calls fetchData. You can better understand this by giving the below example:

onClick={() => this.fetchData("dfd")}

Solution 2 : Use React.MouseEvent

To solve the error you have to just use React.MouseEvent. You can use it in the way given in the below code. So just try this.

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

Solution 3 : Use the correct way

Just change

button.onclick = thisFunction();

to

button.onclick = thisFunction;

Because that is the correct way and it will help you.

Conclusion

So these were all possible solutions to this error. I hope your error has been solved by this article. In the comments, tell us which solution worked? If you liked our article, please share it on your social media and comment on your suggestions. Thank you.

Also Read These Solutions

Leave a Comment