Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Normalize Events with Reacts Synthetic Event System

    Joe MaddaloneJoe Maddalone
    0.14 - 16

    Event handlers are passed an instance of SyntheticEvent in React. In this video we'll take a look at the wide range of events available to us, including Touch events.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    In this lesson, we're going to take a look at React Synthetic Event System, which normalizes all the events we might want to use across various browsers.

    I'm setting up a simple text area here. I'm going to keep the columns and the rows. We're not going to worry about the rest. We'll self-close this guy. Right after that, we're going to have an H1. Its content is going to be this.state.current event.

    Let's go ahead and set up our initial state in our constructor. We'll call super to get our context. We'll say this.state is equal to current event. We'll just set that up with a few dashes.

    We're also going to have an update method. I'm going to go ahead and bind that here to a context of this. We're going to create that method. Now it's going to take in an event off of that text area. We're simply going to set our state of current event to that event type.

    We get all the standard key press events. I'm going to go ahead and assign on key press to this.update. Try that out in the browser. Once we get up there, as soon as we type a key, we get key press. We get all the standard ones, key down, key up and so forth. Let's try a couple more complicated ones.

    We've got on copy, on cut and on paste. I'm going to go ahead and save that. Type some code in here. When we copy, we get our copy event. When we cut, we get our cut event. When we paste, we get our paste event. We can do that with the keyboard shortcuts as well. Here's a copy, here's a cut, and here's a paste.

    We get our standard unfocus and unblur events. When we focus on the field, we get our focus, and when we move away we get our blur, so focus and blur. We get the whole series of mouse events, so mouse over, mouse down and up.

    We also get some more obscure ones. This will be on double-click. When we click in the text area, we're going to get our focus. If I double-click, we get our double-click event. Do that one more time. Double-click and there is our double-click event.

    We also automatically get a series of touch events. I'm going to say, "On touch, start. On touch, move," and, "On touch, end." For this one, we're going to switch over to an iPad emulator. Bring this out a bit.

    Here is our text field. If I touch right there, you'll see I've got a touch start. I'm moving, I get touch move. I get touch end. I'll do that one more time. Touch start, touch move and touch end.