React中 点击事件写法 的注意(this、箭头函数)

目录

‌1、错误写法‌:onClick={this.acceptAlls()}

‌2、正确写法‌:onClick={this.acceptAlls}(不带括号)

总结

方案1:构造函数绑定

方案2:箭头函数包装方法(更简洁)

[方案3:直接绑定 + 参数传递](#方案3:直接绑定 + 参数传递)

方案的关键区别说明


在写代码时,看到别人写的点击事件,大家写法都不太一样,到底有什么需要注意的呢?

-----------需要注意 ‌正确的绑定方式‌ 和 ‌事件触发时机

‌1、错误写法‌:onClick={this.acceptAlls()}

原因:

  • 这样写会‌立即执行acceptAlls 函数,而不是在点击时执行。
  • 括号 () 表示函数调用
  • 组件渲染时就会执行 acceptAlls
  • onClick 实际上接收到的是 acceptAlls 的返回值(这里是 undefined),而不是函数本身
javascript 复制代码
// 例如:错误的写法
class MyComponent extends React.Component {
  acceptAlls = () => {
    console.log("执行逻辑", this.props); //
  };
}


<Button onClick={this.acceptAlls() }>保存</Button>

‌2、正确写法‌:onClick={this.acceptAlls}(不带括号)

javascript 复制代码
// 改正后的:
class MyComponent extends React.Component {
  acceptAlls = () => {
    console.log("执行逻辑", this.props); //
  };
}


<Button onClick={this.acceptAlls}>保存</Button>

总结

在 React 中,onClick 需要接收一个‌函数引用‌(即函数本身),而不是函数的执行结果

  • ‌**onClick={函数}**‌:传递函数引用,点击时执行。
  • ‌**onClick={函数()}**‌:立即执行函数,传递返回值(通常是无效的)。
  • 这里 () => {...} 创建了一个新函数,点击时才会调用 acceptAlls(id)

方案1:构造函数绑定

javascript 复制代码
<Button onClick={this.acceptAlls }>保存</Button>

上述写法的前提需要:this指向要明确(要么手动绑定、要么写成箭头函数)

javascript 复制代码
class MyComponent extends React.Component {
  // 方式1:构造函数中绑定
  constructor(props) {
    super(props);
    this.acceptAlls = this.acceptAlls.bind(this);  // !!!!!!
  }
  acceptAlls() {
    console.log("执行逻辑", this.props); // this 正确指向组件实例
  }

    <Button onClick={this.acceptAlls}>直接确认</Button >
}

javascript 复制代码
class MyComponent extends React.Component {
  // 方式2:箭头函数自动绑定 this
  acceptAlls = () => {
    console.log("执行逻辑", this.props); // this 始终正确
  };

    <Button onClick={this.acceptAlls}>直接确认</Button >
}

javascript 复制代码
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.acceptAlls = this.acceptAlls.bind(this); // ✅ 正确绑定
  }
  
  acceptAlls(id) {
    console.log("执行ID:", id, this.props); // ✅ this 正确
  }
  
  render() {
    return (
      <Button onClick={() => this.acceptAlls(item.id)}>确认</Button>
      // ✅ 箭头函数传参 + 已绑定的方法
    );
  }
}

方案2:箭头函数包装方法(更简洁)

javascript 复制代码
class MyComponent extends React.Component {
  // 箭头函数自动绑定 this
  acceptAlls = (id) => {
    console.log("确认操作", id, this.props);
  };

  render() {
    return (
      <div>
        {/* 无参数 */}
        <Button onClick={this.acceptAlls}>直接确认</Button >
        
        {/* 有参数 */}
        // 需要传递额外参数时(如 id):
        <Button onClick={() => this.acceptAlls(item.id)}>确认</Button>
      </div>
    );
  }
}

注意:

  • 箭头函数一定的性能影响‌,每次渲染都会创建新函数,可能影响性能(在循环或纯组件中慎用)‌

方案3:直接绑定 + 参数传递

javascript 复制代码
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.acceptAlls = this.acceptAlls.bind(this);
  }
  
  acceptAlls(id) { /*...*/ }
  
  render() {
    return (
      <Button onClick={this.acceptAlls.bind(this, item.id)}>确认</Button>
      // ✅ bind 直接绑定参数(注意性能影响)
    );
  }
}

方案的关键区别说明

方案 this 绑定 参数传递 性能影响
构造函数绑定 手动绑定 需箭头函数传参 最优
箭头函数方法 自动绑定 可箭头函数传参 较优
直接bind传参 手动绑定 bind直接传参 较差(每次渲染新建函数)

最佳实践建议

  1. 优先使用方案2(箭头函数方法) ‌:既解决this问题,又保持代码简洁
  2. 需要兼容旧代码时用方案1‌:显式绑定更易理解
  3. 避免在render中直接bind‌(方案3):可能引发性能问题
相关推荐
波波鱼દ ᵕ̈ ૩21 分钟前
学习:JS[6]环境对象+回调函数+事件流+事件委托+其他事件+元素尺寸位置
前端·javascript·学习
cypking1 小时前
解决electron+vue-router在history模式下打包后首页空白问题
javascript·vue.js·electron
climber11211 小时前
【Python Web】一文搞懂Flask框架:从入门到实战的完整指南
前端·python·flask
Watermelo6171 小时前
极致的灵活度满足工程美学:用Vue Flow绘制一个完美流程图
前端·javascript·vue.js·数据挖掘·数据分析·流程图·数据可视化
门前云梦1 小时前
ollama+open-webui本地部署自己的模型到d盘+两种open-webui部署方式(详细步骤+大量贴图)
前端·经验分享·笔记·语言模型·node.js·github·pip
Micro麦可乐1 小时前
前端拖拽排序实现详解:从原理到实践 - 附完整代码
前端·javascript·html5·拖拽排序·drop api·拖拽api
Watermelo6171 小时前
Web Worker:让前端飞起来的隐形引擎
前端·javascript·vue.js·数据挖掘·数据分析·node.js·es6
Micro麦可乐1 小时前
前端与 Spring Boot 后端无感 Token 刷新 - 从原理到全栈实践
前端·spring boot·后端·jwt·refresh token·无感token刷新
Heidi__1 小时前
前端数据缓存机制详解
前端·缓存
讨厌吃蛋黄酥1 小时前
前端路由双雄:Hash vs History,谁才是React项目的真命天子?
前端·react.js·设计