Hello, in this tutorial, we'll learn how to add events to your HTML elements so users can interact with your application.
Hello, in this tutorial, we'll learn how to add events to your HTML elements so users can interact with your application.
Adding Events
React has the same events that normal DOM elements have.
To add these events, just include them in the open HTML element tag, followed by "=" and a pair of open and closed curly brackets like this:
<h1 onClick={ }>Codeible</h1>
In the example, a onClick event is added to the h1 element. So when you click on it, it'll execute whatever it is inside the curly brackets.
Handling the Events
In the above section, you learned how to add a event to an element. Now, we'll see how to handle the event. This means to define what will happen when the event is triggered.
<h1 onClick={ ( event ) => { alert("Click Triggered"); } }>Codeible</h1>
So when the h1 element is clicked on, it'll execute the function that we inserted in the curly backets and display the alert popup.
For this method, it is similar to method 1 except the handler function is defined outside of the HTML.
That is all for this tutorial. Now you are able to add events to your HTML so people can interact with your app!