【问题标题】:How to call two functions in one onClick handler [duplicate]如何在一个 onClick 处理程序中调用两个函数[重复]
【发布时间】:2018-03-06 19:40:41
【问题描述】:

我希望将另一个函数传递给已经传递onClick 道具的子组件,以便在单击它时能够禁用它。我知道我应该将当前的 onClick 操作提取到一个单独的函数中,并在那里实现禁用操作,但是当我提取它时我遇到了 this.props.onClick (func PropType) 正确执行的问题。有没有办法在不使用单独函数的情况下解决它,或者我在提取它时只是不正确地调用this.props.onClick

以下是我正在使用的当前代码(未实施禁用操作)

render() {
    const b = block('btn').mix('btn').mix('action-button');

    return (
      <button
        onClick={this.props.onClick}
        className={b(this.props.className)}
        title={this.props.title}
      >
        {this.props.buttonText}
      </button>
    );
    }

【问题讨论】:

  • 不能只调用点击事件处理程序中的第二个函数吗?
  • 我猜这是个愚蠢的问题 - 但我该怎么做呢?
  • 不确定,提供一些代码。

标签: javascript reactjs


【解决方案1】:

我不确定我是否完全理解你的问题,但是在你的类上创建一个单独的方法会有所帮助

    handleClick () {
       this.props.onClick()
       otherOnClickFunc()
    }

并传递您的处理程序 this.click() 而不是 this.props.onClick()

【讨论】:

  • (确保将this.click = this.click.bind(this) 添加到您的构造函数中)
猜你喜欢
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2017-01-22
  • 2012-12-01
  • 2017-11-18
  • 2021-01-11
相关资源
最近更新 更多