In React is it better to think of onClick as setting an eventListener rather than as a standard JavaScript onclick attribute?
For example, consider these two code snippets, the first in standard JavaScript, the second in React:
<button onclick="activateLasers()">
Activate Lasers
</button>
<button onClick={activateLasers}>
Activate Lasers
</button>
In the top example, onclick is associated with an express CALL TO the activateLasers function. However, in the React example, onClick is simply associated with the activateLasers function (the syntax is not expressly calling the function, even though behind the scenes it does call it). In this sense, in spite of its name React's onClick is more like this conventional JavaScript code:
.addEventListener("click", activateLasers)
Or is this the wrong way to think about it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…