Using Event Handlers in React
Tags: react, javascript, eventhandler, event
Published: 2020-07-13 03:34:09

Hello! This week I'm going to be covering how to use event handlers in React. Well first lets talk a little bit about what events are in JavaScript. [Jennifer Fu](https://medium.com/better-programming/whats-the-difference-between-synthetic-react-events-and-javascript-events-ba7dbc742294) described it well in her blog [What’s the Difference Between Synthetic React Events and JavaScript Events?](https://medium.com/better-programming/whats-the-difference-between-synthetic-react-events-and-javascript-events-ba7dbc742294) when she said: "[JavaScript events are actions or occurrences that happen on web pages. They are an essential part of an interactive user interface.](https://medium.com/better-programming/whats-the-difference-between-synthetic-react-events-and-javascript-events-ba7dbc742294)" Events are what make a webpage interactive. Anytime the user mouses over a part of the website, an event is created. The same is true for anything ranging from clicking on an image to typing in a text box or even the loading of the page. The origin of these events come from HTML and it's JavaScript's job to know how to listen for them and then handle them.

When using event listeners in JavaScript, they are written out as an HTML attribute with all letters in lowercase.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gimdurn1yv86gzwj12r9.png)

React takes a different approach to their event listener syntax. React uses the camelCase style.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4ppr2kba660tfgcoykdc.png)

You may have also noticed the curly braces around the event handler name as well which is in contrast to JavaScript's double quotes. This is another important difference between the React and JavaScript syntax. 

React uses the ES6 style for creating their components. Each component is created using the 'class' keyword so when event handlers are created, they must be methods on a class component. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/do1hig0zva1uxkoh4r36.png)

It is also important to realize that any event handlers that are created will need to be bound to the component that created them. If not when you pass the event handler to the event listener in the HTML, the binding of 'this' will be lost and when the interpreter encounter's the key word 'this', ol' 'undefined will be returned😢. 

There are a couple of different ways to approach this. If we are using the method creating technique applied in the last example, then we would need to bind the event handler in the constructor. This way we produce a new function that will always refer to the component that created it no matter what context it is used in.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oknlarpwlg3uvo6itpho.png)

Now when I click on my button that logs 'this', you can see the App component was logged to the console.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7q8vborfxsl1k63tlyvw.png)

Another approach is to use arrow function syntax when you are creating your methods/event handlers. [W3Schools](https://www.w3schools.com/react/react_events.asp) states that ["With arrow functions, this will always represent the object that defined the arrow function."](https://www.w3schools.com/react/react_events.asp)
Since in React we always want 'this' to refer to the component that the method is found on, the arrow technique works as well.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u5qaduor3xkg069w5rra.png)

Once you have your event listener/handler set up, it's time to start using it! One popular way to use an event handler is to have a state property set to a boolean value and then use your handler to toggle that state value. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y6uhimvukf3e7t7e0sty.png)

Now that I have this set up, you can see that I get true/false toggled in the console.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dl82cwxu6cffyg2djv9g.png)

This technique can then be further used to render something to the page based on the state of the conditional.

Another popular technique is to use an event handler to update the state to represent the value from a text box.  You could then for example, make a 'GET' request with that value.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3frfgr1dlvmdal24x0ud.png)

In this example, I make use of the event object parameter and then extract the value from it by using event.target.value and then update the state with that new value. You may be wondering where is this 'event' object coming from. According to [David Wall](https://www.itworld.com/article/2784331/synthetic-events.html) in his article about [synthetic events](https://www.itworld.com/article/2784331/synthetic-events.html) he states that "[When the user clicks a button, to cite a popular example, the click is represented as an event object of the click type and is passed to whatever event handler (if any) is assigned to react to events of that kind.](https://www.itworld.com/article/2784331/synthetic-events.html)" So we can then access this event object and extract values from it. One way of doing this is by accessing the event.target.value property which in this case gives us the value from the input text box. Since we have an event listener of 'onChange' set up to call our event handler method, whenever the text input value changes in the text box the state is updated and the new state value is then logged to the console.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8kg3r90oodql1os94cou.png)

In conclusion, React makes it intuitive and easy to use event handlers. Just remember to use ES6 syntax and add them inside of the class like you would any other method. Don't forget to bind them to the component that created them by either using arrow functions or by binding them in the constructor. Good luck and happy eventing!🐱‍🏍