react中onclick
A handleClick event wasn’t behaving like I expected it to behave. I replaced everything inside it with a console.log
to make it easier to tell what was going on.
handleClick事件的行为不像我预期的那样。 我用console.log
替换了其中的所有内容,以便更轻松地知道发生了什么。
When the component loaded, the event fired many times.
加载组件后,该事件将触发多次。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/e2fdb8583bbd68ba4ae13a1832e6ab58.png)
This indicated to me that the function was being run many times when the component was loaded.
这向我表明该函数在加载组件时正在运行多次。
If the function was not running at all, I would have put this
s on things until it worked. Because it was running too many times, I removed ()
s until it worked.
如果函数不是在所有运行,我将不得不把this
S于事,直到它的工作。 因为它运行了太多次,所以我删除了()
直到起作用。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/b7af78a87114c8832ba83a149a05856b.png)
Now it only poop
s when I click on the div. Success.
现在,只有当我单击div时,它才会poop
。 成功。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/6828acc2f0b93582e70b711e8a6a0f2e.png)
What are your best tips and tricks for debugging React?
您调试React的最佳秘诀是什么?
翻译自: https://medium.com/@merxplat/debugging-a-problem-with-an-onclick-event-in-react-js-8096d6c28c8c
react中onclick