首页 > 其他 > 详细

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

时间:2019-11-14 12:53:31      阅读:83      评论:0      收藏:0      [点我收藏+]
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;

 

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

原文:https://www.cnblogs.com/llcdbk/p/11855507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!