How to resolve 'Uncaught TypeError: this.props.onClick is not a function' in JavaScript?
1. Purpose
In this post, I will demonstrate how to resolve the following issue when trying to call a function from a child component to a parent component in React.js. This problem is common in the JavaScript world.
The core error message is:
And this error is shown on the page:
2. Environment
- npm and node version:
3. The code and the solution
3.1 The code that caused the problem
I was writing a tic-tac-toe game by following the React tutorial. When it came to calling a function from a child to a parent component, where we need to pass a function from the parent to the child, the error occurred.
The program can be illustrated as follows:
The error occurred when the user clicked any of the squares:
which would call the function:
and the this.props.onClick
is passed from the parent component as follows:
You can see that we passed a function
to onClick
of the Square component, so why did the error happen?
3.2 The solution to this problem
The solution is straightforward. Just change the functions in the Board component as follows:
First, we need to turn onSquareClicked
into a normal function, which has a single parameter to track which square is clicked and act on the event.
Second, we change the onClick
property to use an arrow function, which has no parameters and just calls onSquareClicked
.
3.3 Why did this happen and why the solution works?
This error occurred because we were calling the function instead of passing the reference of the function to the child.
According to this document, the error TypeError: "x" is not a function
happens when:
It attempted to call a value from a function, but the value is not actually a function. Some code expects you to provide a function, but that didn’t happen.
Maybe there is a typo in the function name? Maybe the object you are calling the method on does not have this function? For example, JavaScript
Objects
have nomap
function, but the JavaScriptArray
object does.There are many built-in functions in need of a (callback) function. You will have to provide a function in order to have these methods working properly.
The solution works because we need to pass a function’s reference to the child, but we passed a function result
to the child, which is not a function by itself. We use an arrow function because the arrow function returns a function reference instead of calling the function.
4. Summary
In this post, I demonstrated how to resolve the ‘TypeError: “x” is not a function’ error when trying to pass a function to components in React.js. The key point is to pass the function reference, which can be achieved by using arrow functions. This ensures that the child component receives a function reference rather than the result of a function call. By following this approach, you can avoid common pitfalls when working with function passing in React.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!