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.