Giter Site home page Giter Site logo

react-event-system-dc-web-111918's Introduction

React Event System

Overview

In this lesson, we'll cover the event system in React.

Objectives

  1. Explain how React events differ from browser events
  2. Describe how React standardizes events for compatibility
  3. Explain how to use React events in our application
  4. Add event handlers to an element

What's the event system in React?

React has its own event system with special event handlers called SyntheticEvent. The reason for having a specific event system instead of using native events is cross-browser compatibility. Some browsers treat events differently, and by wrapping these events into a consistent API, React makes our lives a lot easier. It's important to keep in mind that they are the exact same events, just implemented in a consistent way! That means these events also have methods that you can call like preventDefault(), stopPropagation(), and so on.

How to add event handlers

Consider the following component:

class Tickler extends React.Component {

  tickle = () => {
    console.log('Tee hee!');
  }

  render() {
    return (
      <button>Tickle me!</button>
    );
  }
}

We have a tickle() function, but no way to trigger it! This is a perfect time to add an event handler so that we can see the message in our console. We attach event handlers to an element much like how we'd add a prop. The handler name is always comprised of on, and the event name itself โ€” for example click. These are joined together and camel-cased, so if we wanted to add a click handler, we'd call the prop onClick. This prop takes a function as a value โ€” it can either be a reference to a method on the class (like our tickle() method), or an inline function. Most of the time, we'll use a function reference. It looks like this:

<button onClick={this.tickle}>Tickle me!</button>

As you can see, we're passing a function reference, and not executing the tickle function. Now, when we click the button, we see a message in our console. Awesome! Going back to the complete example, let's take a quick look at the other code living there. You will notice our tickle() method is an arrow function. You will commonly see React component methods defined with arrow functions. This is because we often want to access the this keyword within the methods themselves. By using an arrow function, we avoid creating a new scope with a different value of this. Note that this is not required in the above example (since we're not accessing the component's this). Realistically, most methods in a React component class will use this in one way or another, so it's a good idea to get in the habit of writing your methods as arrow functions.

There are a lot of event handlers we can add to an element, for example onKeyUp, onMouseDown, onFocus, onSubmit, and many more. Check out the complete list of supported events to see what else you can play around with!

Resources

View Event System on Learn.co and start learning to code for free.

react-event-system-dc-web-111918's People

Contributors

gj avatar thomastuts avatar lukeghenco avatar thuyanduong-flatiron avatar annjohn avatar ivalentine avatar pletcher avatar telegraham avatar tsengsational avatar joelachance avatar maxwellbenton avatar bperl avatar

Watchers

 avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.