React bind(this) 参数详解

<span onClick={this.historyMaterial.bind(this, materialName)} className={'label label-warning materialStatus'}>材料已更新点击查看历史材料</span>

 此时想 把参数 materialName 传递给方法 historyMaterial

在查询资料之前一直以为传参和接受参数的位置是一一对应的,今天查了资料才发现reac中的bind()有点不同

 

historyMaterial = (materialName, evt) => {
        const brotherOfMaterial = evt.target.nextSibling;
        const classList = rclUtil.getDeepVal(brotherOfMaterial, 'classList.value');
        if(classList && classList.indexOf('hide') > -1){
            brotherOfMaterial.classList.remove('hide');
        }else{
            brotherOfMaterial.classList.add('hide');
        }
        
        console.log('historyMaterial ->', evt, materialName);

    }

接受参数是第一个,而当前点击对象是后面的evt,正好相反

结论:react 通过 bind 方法来绑定参数,第一个参数指向 this,第二个参数开始才是事件函数接收到的参数

事件:this.handleclick.bind(this,要传的参数)

函数:handleclick(传过来的参数,event)

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值