Categories
React Answers

How to fix Expected onClick listener to be a function, instead got type string error with React?

Spread the love

Sometimes, we want to fix Expected onClick listener to be a function, instead got type string error with React.

In this article, we’ll look at how to fix Expected onClick listener to be a function, instead got type string error with React.

How to fix Expected onClick listener to be a function, instead got type string error with React?

To fix Expected onClick listener to be a function, instead got type string error with React, we should make sure we set the onClick prop to a function reference.

For instance, we write

const activateLasers = () => {
  //...
};

<button onClick={activateLasers}>Activate Lasers</button>;

to set the onClick prop to the activateLasers function.

We don’t call the function.

We just set the function reference as the onClick prop’s value.

Conclusion

To fix Expected onClick listener to be a function, instead got type string error with React, we should make sure we set the onClick prop to a function reference.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *