React中绑定事件

1.事件的名称都是React提供的,因此名称的首字母必须大写:onClick、onMouseOver

2.为事件提供的处理函数必须是如下格式:

onClick = { function }

3.用的最多的事件绑定形式为:

<button onClick={ () => this.show('传参') }>按钮</button>
 
show = (arg1) => {
        console.log('show方法' + arg1);
    }
}

例如:

import React from 'react';

export default class BindInputValue extends React.Component{
    constructor(){
        super();
        //私有数据
        this.state = {}
    }

    render(){
        return <div>
            BindInputValue组件
            <hr/>            
            {/* 注意:onClick只接收function作为处理函数(箭头函数就是匿名的function函数) */}
            <button onClick={()=>{this.myclickHandler('123')}}>按钮</button>
        </div>
    }

    //这是一个实例方法
    myclickHandler = (arg1) => {
        console.log('ok' + arg1);
    }
}

猜你喜欢

转载自www.cnblogs.com/zcy9838/p/12030812.html