Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1 class="title">React Click Event</h1>
<h4 class="sub-title">3 different ways to do the click events in React</h4>
<p class="desc"><a href='https://reactjs.org/docs/handling-events.html'>Refrence</a></p>
<div id="root"></div>
              
            
!

CSS

              
                body {
  background-color: #efefef;
  font-family: sans-serif;
  line-height: 1.3em;
  font-size: 16px;
}
              
            
!

JS

              
                class App1 extends React.Component {
  constructor(props) {
    super(props);
    // If we comment out the following line we will get run time error when changeColor get called.
    this.changeColor = this.changeColor.bind(this);
  }

  changeColor(e) {
    e.currentTarget.style.backgroundColor = "#00FF00";
    console.log(this.props);
  }

  render() {
    return (
      <div>
        <p> This is the default way, we need bind `this` to function in constructor </p>
        <button onClick={this.changeColor}> button 1</button>
        <span>         </span>
        <button onClick={this.changeColor}> button 2</button>
      </div>
    );
  }
}

class App2 extends React.Component {
  changeColor = e => {
    e.currentTarget.style.backgroundColor = "#00FF00";
    console.log(this.props);
  };
  render() {
    return (
      <div>
        <p> When we define the function as a class field, we don't need to bind `this` </p>
        <button onClick={this.changeColor}> button 1</button>
        <span>         </span>
        <button onClick={this.changeColor}> button 2</button>
      </div>
    );
  }
}

class App3 extends React.Component {
  changeColor(e, colorHex) {
    e.currentTarget.style.backgroundColor = colorHex;
    console.log(this.props);
  }
  render() {
    return (
      <div>
        <p> When we use arrow function, we don't need to bind `this` and can pass parameters</p>
        <button onClick={e => this.changeColor(e, "#ff0000")}> button 1</button>
        <span>         </span>
        <button onClick={e => this.changeColor(e, "#0000ff")}> button 2</button>
      </div>
    );
  }
}

ReactDOM.render(
  <div>
    <App1 />
    <App2 />
    <App3 />
  </div>,
  document.getElementById("root")
);

              
            
!
999px

Console