React JS onClick event handler

前端 未结 11 1148
孤街浪徒
孤街浪徒 2020-11-28 21:35

I have

var TestApp = React.createClass({
      getComponent: function(){
          console.log(this.props);
      },
      render: function(){
        retur         


        
11条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 21:59

    Use ECMA2015. Arrow functions make "this" a lot more intuitive.

    import React from 'react';
    
    
    class TestApp extends React.Component {
       getComponent(e, index) {
           $(e.target).css({
               'background-color': '#ccc'
           });
       }
       render() {
           return (
               
    • this.getComponent(e, 1)}>Component 1
    • this.getComponent(e, 2)}>Component 2
    • this.getComponent(e, 3)}>Component 3
    ); } }); React.renderComponent( , document.getElementById('soln1'));`

提交回复
热议问题