Forget about the reaction first:
This is not related to the reaction and allows us to understand the basic concepts of Java Script. For example, you wrote the following function in a Java script (name A).
function a() { };
Q.1) How to call the function that we defined?
Answer: a ();
Q.2) How to pass a link to a function so that we can call it later?
Answer: let the fun = a;
Now, going to your question, you used paranthesis with the name of the function, which means that the function will be called when the following statement is executed.
<td><span onClick={this.toggle()}>Details</span></td>
Then how to fix it?
Just!! Just remove the brackets. So you gave a link to this function to the onClick event. It will only call your function when you click on your component.
<td><span onClick={this.toggle}>Details</span></td>
One proposal has been issued to react:
Avoid using the built-in feature suggested by someone in the answers, this can lead to poor performance. Avoid the following code, it will instantiate the same function again and again each time the function is called (the lamda statement creates a new instance each time).
Note: and there is no need to explicitly pass event (e) to the function. You can access it in a function without passing it.
{<td><span onClick={(e) => this.toggle(e)}>Details</span></td>}
https://cdb.reacttraining.com/react-inline-functions-and-performance-bdff784f5578
source share