react 中事件参数和一般参数以及this绑定的写法

import React from 'react';
import PropTypes from 'prop-types';
class Home extends React.Component{
    render(){
       return (
           <div>
                <h1>hello word</h1>
                <button onClick={(event)=>this.TestMethod(event,"哈哈哈")}>
                    click me
                </button>
           </div>
       )
    }
    //也可以用构造函数来表示this,如果用正常的函数体写的话,this是不能表示出来的,这里的this 指的是home 这个类,因为箭头函数没有作用域
    TestMethod=(e,arg1)=> {
        console.log('e is :',e);
        e.preventDefault();
        console.log('链接被点击');
        console.log('this is :',this);
        console.log(arg1);
    }
}


Home.ProtoTypes={
    sex:PropTypes.number
}
export default Home;
原文地址:https://www.cnblogs.com/llcdbk/p/11855507.html