02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Working with Events in React

In this tutorial, we dive into handling events in React, focusing on key concepts such as event binding, managing scope with ES6 features, and preparing for state management.


1. Setting Up an Event Handler

To handle a button click, we created an onClick method in the App component:

class App extends React.Component {
    onClick(event) {
        console.log("Hello, world!");
    }

    render() {
        return <button onClick={this.onClick}>Click Me</button>;
    }
}

2. The Problem with this in Event Handlers

By default, the value of this inside the onClick method does not refer to the component instance. This happens because JavaScript's this is dynamic and changes based on how the function is called.


3. Using a Constructor to Bind this

To ensure this refers to the component instance, we bind the method in the constructor:

constructor(props) {
    super(props);
    this.onClick = this.onClick.bind(this);
}

This guarantees that this always points to the component, even when the method is used as an event handler.


4. Explanation of the Constructor and super

  • The constructor initializes the component instance.
  • super(props) passes the props to the parent class (React.Component) and sets up the React component lifecycle.
  • Binding the onClick method in the constructor ensures it retains the correct this reference.

5. Using ES6 Arrow Functions for Simplicity

An alternative to binding in the constructor is to use an arrow function, which automatically binds this:

class App extends React.Component {
    onClick = (event) => {
        console.log("Hello, world!");
    };

    render() {
        return <button onClick={this.onClick}>Click Me</button>;
    }
}

Key Takeaways

  1. Event Handling: React makes it simple to add event listeners via JSX attributes like onClick.
  2. Scope Management: Use bind in the constructor or ES6 arrow functions to avoid scope issues with this.
  3. Preparation for State: Events are essential for interacting with and updating a component’s state, which will be explored in the next lesson.

This tutorial established a foundation for handling events in React. In the next session, we’ll explore React state and learn how events can dynamically update the UI.

Video Transcript

00:00While this lecture on its own doesn't really mean much
00:03because we're gonna create an event.
00:05We're gonna see how events work and react,
00:07but it's really gonna be a preparation
00:09for the following lectures
00:10as we take a look at constructors scope and even events.
00:14So let's jump right into it and let's prepare our groundwork
00:17for the state, which will be explored in the next section.
00:21Now, although we didn't talk about it,
00:23our buttons are clickable if we chose
00:26to make them clickable very easily.
00:28And I wanna show you how you would do that.
00:30Now, I'm not gonna put any clickable events within the
00:33button, namely because I want our button
00:35to be completely agnostic to our application.
00:38And I'm gonna put our button event
00:40actually in our app itself.
00:41And to do that, I'm gonna create a new
00:43method called on Click.
00:45And this method has an event like any other click event.
00:49And I'm gonna go to our button, let's say
00:51to our primary button there, the large one.
00:53And I'm gonna add an on click, click on click event.
00:58And I'm gonna set it to be this dot on click.
01:01Now that's all I really had to do.
01:03And really if I just go to my application
01:05and click on on it, we should once everything we renders,
01:12oh, let me put your a trace,
01:14there's definitely nothing going on.
01:15So what would we say? Console log. Hello.
01:19Let's give it a run. And now if I go ahead
01:22and click on our button,
01:23then we'll see that we're gonna get that.
01:25Hello. Beautiful. Our low is clicking through.
01:28Now if I go ahead and I try to see what is, oh, even
01:31before I do that, let me just explain to you
01:32what is actually going on here.
01:35Even though our button does not have this functionality
01:37period, it just does not
01:39because our button js itself, what's hack actually
01:43to happening to the props, they're going to our tag.
01:46And because our tag is gonna be, or a button
01:48or an A ref, it's gonna basically create that on
01:52that HTML directly.
01:53The on click, which is a native JavaScript controller.
01:56So this is happening automatically
01:58'cause we're passing it through.
01:59So literally our button is gonna pass
02:01through whatever we put here.
02:02If it's on, click on rollover,
02:03on whatever we might put there.
02:06Now with that said, if we take a look at our this,
02:10we'll notice that our, this is not gonna be comparable
02:12to the this of our full component,
02:14which is usually a big no-no when you're working and react.
02:18And generally speaking, you want
02:19to keep your scope in E ES six.
02:21And in general, you wanna keep your this the same within
02:24your methods, even if you're sending them around.
02:26So what I'm gonna do to address this problem with this,
02:29this has also introduced you to something that's happening
02:32behind the scenes with React.
02:34In general, when you're creating these JSX components,
02:37what's really happening is a new X,
02:40whatever his name is created and the props are sent through.
02:44Now these props are sent through all the way
02:45to our components itself.
02:47So what I'm gonna do is I'm gonna go ahead and create that.
02:50It's like a method, a function that is called
02:53whenever our application,
02:55whenever our component is being created, it's called a
02:58Constructor and it's associated with any class.
03:01So in the process of creating a new object,
03:04the class calls its constructor truck tour.
03:07When the constructor is then called the pro, the the methods
03:11and whatnot that were set would be sent over.
03:13And in our case, we don't really wanna do anything
03:16with them, we just wanna make sure
03:17that our react component will fetch those
03:20component properties.
03:21So we'll know what to do with them.
03:23So in this case, I'm just gonna go ahead and call super.
03:26And Super is literally a command that says, go
03:29to my extended parent, go to that parent
03:31and send to its constructor.
03:33Whatever I want to send to. In this case, I want
03:35to continue sending the props without touching them.
03:38Once I've done that, now I could go back to this problem
03:40that I'm having with this, this on the on click
03:43and use a new ES six uh, feature.
03:46By going to this on click
03:48and changing its definition by hard coding
03:51who it's, this will be.
03:53So I'm basically saying on click equals not
03:55on chick on click.
03:57And I wanna bind it to itself to the current this
04:00'cause while we're in the constructor, we definitely know
04:02what this is, that this is our object
04:04that is being created right now while in our on click,
04:08it's getting lost because it's being sent around.
04:10So this binding
04:11that we're doing here is basically telling on click.
04:14By the way, anytime I say this, I mean the object,
04:17this explicit object.
04:20Now if I go back to that to my app
04:22and I click here on ref again, we'll see
04:23that now we're gonna get our object every single time we
04:26click on it, which is exactly what we need for us to be able
04:30to do our next step, which our next step will relate
04:33to actually state.
04:35And we're gonna take a look at what that is.
04:37One of the last big topics in React that you need
04:40to know about in this lecture.
04:42We went ahead and really set up the groundwork for events
04:46and are now triggering them.
04:47But what we really wanna do is we want to change things
04:50inside of our component.
04:51Now, to do that, we can't use props,
04:54we can't approach directly our elements,
04:56but we're gonna have to use state, which is something
04:58that we're gonna explore in the next lecture.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!