Languages

React - why does onClick function fires on component render?

0 points
Asked by:
Savannah
559

Why is onClick function being executed on component render?

I have code similar to this:

And the alert() fires before I even click on the button element.

1 answer
0 points
Answered by:
Admin Dirask Community
4380

What you are doing wrong is calling the function instead of passing the function to onClick property.

Quick solution:

0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join