React supports events that are supported in JavaScript, and it works similarly, but the significant difference is its way of using the events. There are two significant differences:
eventName()
.JavaScript events are used to perform various operations based on events such as click, touch, up, and down. All the events, called synthetic events, can be used in React. This guide will demonstrate how to use plain JavaScript events within React components.
Unlike JavaScript, React does not allow you to use event names as you do with JavaScript, like onclick()
; hence you need to follow the event name as onClick()
.
You can still use plain JavaScript events using the window
object used to represent the DOM document opened in the current browser window.
A click event is the most common event used to trigger a function based on the click event on the elements such as button, label, span, link, etc. To add the click event in React using plain JavaScript, you need to use addEventListener()
to assign the click event to an element.
Create one <button>
element as ref
props so that it can be accessed to trigger the click event.
1render() {
2 return (
3 <>
4 <button ref={mybutton => (this.btn = mybutton)}>Click me</button>
5 </>
6 );
7}
The <button>
element has additional props called ref
that are used to find the element from the DOM. To assign the JavaScript click event handler, you can implement it as demonstrated below.
1componentDidMount() {
2 this.btn.addEventListener("click", this.onButtonClick);
3}
addEventListener()
assigns the event handler and the event name to do an activity once the button gets clicked.
1onButtonClick() {
2 alert("Clicked on Button");
3}
Once you click the button, it will show the alert box that the click event is triggered.
The change event triggers the event based on the existing value and processes changes based on the changed value. Using the addEventListener()
, you can assign change
as the event handler followed by the event handler function as given below.
1componentDidMount() {
2 this.input.addEventListener("change", e => this.onChange(e));
3}
Then, you can implement the event handler function to process the updated values.
1onChange(e) {
2 console.log("Updated values is :-", e.target.value);
3}
Alternatively, you can remove the listener using document.getElementByID()
along with the addEventListener()
as given below.
1document.getElementById("#your_id").addEventListener("change", this.onChange);
So far, this guide has shown how to define the JavaScript event handler and associated event handler function. But you should remove the listener once the current component gets unmounted from the DOM.
To remove the event listener, you can use removeEventListener()
along with the event type and the event handler function.
1componentWillUnmount() {
2 this.btn.removeEventListener("click", this.onButtonClick);
3 this.input.removeEventListener("change", e => this.onChange(e));
4}
As you can see in the above code, the componentWillUnmount()
function is implemented for anything that needs to get removed, such as promises and events.
Inside the function, two events removed are from the DOM, click
and change
, followed by the event handler function, so you can use the removeEventListener()
to remove the attached events from the component.
Alternatively, you can remove the listener using document.getElementByID()
along with removeEventListener()
.
1document.getElementById("#your_id").removeEventListener("change", this.onChange);
React supports most of the JavaScript events but with a different version of the naming convention; hence, you should only use the React-based naming convention and not the plain JavaScript events with the React elements.
Events handle the element's response based on actions such as click, touch, drag, up, down, etc. To perform specific events, you can use various React synthetic events to trigger any actions.