In this article, We will try to understand the use of the onClick event handing in-depth for React. In any type of application, Event handlers enable the user to interact with the application using clicking, typing, scrolling the page, etc.
If you have an understanding of working with HTML & plain JavaScript, then you can easily play with event handings to React. In React, Events are declared in camelCase. For example, You have to perform any specific task on clicking on any HTML element then declare the onClick method to React.
index.html
script.js
In this example, we will see the very basic use of onClick event in the React application
In this example, We will write the function just after the onClick event, This type of approach can be used for very basic task, like showing alert, assigning value to a variable, etc.
Sometimes we need to call two different functions to perform two different tasks on the same event. This can be easily handled with the below example:
In this example, We will declare an array with name of countries and get the array items using map() and show the item using inline onClick event handler.
In this article, We learn about the use of onClick event hander in-depth with Example in React
I hope this article helped you to get in depth knowledge about the topic. You can also find other demos/articles at React.js Sample Projects here to start working on enterprise-level applications.
Let me know your thoughts over email demo.jsonworld@gmail.com. I would love to hear them and If you like this article, share it with your friends.
Thank You!