如何在React.js中向同一事件添加多个事件处理程序

时间:2015-10-28 18:15:52

标签: event-handling reactjs

所有

我想知道是否有可能将多个事件处理程序绑定到同一事件?

例如:

var LikeToggleButton = React.createClass({
    render: function(){
        (function toggle(){
            this.setState({liked:!like});
        }).bind(this);
        return (
            <div onClick={toggle}>TOGGLE LIKE</div>  
        );
    }
});

在此之前,一切似乎都正常,但我想为该按钮添加另一个功能,由其他选项决定:

例如,我有另一个开关组件(可能是复选框或单选按钮等),名为&#34; count toggle&#34;,启用时,LikeToggleButton的按钮将添加另一个onClick处理器开始计算点击按钮的时间,我知道它可以预先设置到切换功能,但我只是想知道是否有办法将此部分附加到onClick处理程序?

谢谢

4 个答案:

答案 0 :(得分:6)

如果你想在触发onClick时执行多个回调,你可以从外面传递它们,这样你就可以在props对象中访问它们。然后执行所有操作(注意:代码未经测试):

var LikeToggleButton = React.createClass({

    toggle: function() {
        this.setState({liked:!like});
    },

    handleClick: function(e) {
        e.preventDefault();
        this.toggle();
        for (var i=0, l<this.props.callbacks.length; i<l; i++) {
           this.props.callbacks[i].call();
        }
    },

    render: function() {            
        return (
            <div onClick={this.handleClick}>TOGGLE LIKE</div>  
        );
    }
});

但是,如果你想在它们之间连接组件,你不应该通过调用处理程序内的方法来做到这一点。相反,你应该使用一种架构模式,其中Flux是显而易见的选择(但还有更多)。

看看Flux,这里有more choices

答案 1 :(得分:2)

对于不需要组件了解使用它的组件的可扩展方式 - 在更改之前保存onClick事件。 这是从实际工作代码中提取的重点:

button.jsx

class Button extends React.Component {

  constructor(props) {
    super(props);
    this.state= { callback: false};
  }

  click(){
    //do stuff here
    if(this.state.callback) { this.state.callback.call(); }
  }

  render () {
    this.state.callback = this.props.onClick; // save the onClick of previous handler

    return (
      <button { ...this.props } type={ this.props.type || "button" } onClick={ this.click.bind(this) } className = this.props.className } >
        { this.props.children }
      </button>
    );
  }
}
export default Button;

然后在另一个组件中,您可以使用该按钮,它可以拥有它自己的onClick处理程序:

class ItemButtons extends React.Component {
  itemClick () {
    //do something here;
  }

render () {
    const buttons = [
      (
          <Button onClick={ this.itemClick.bind(this) } className="item-button">
            <span>Item-Button</span>
          </Button>
      )
    ];

    return (<section>{ buttons }</section>);
}

export default ItemButtons;

答案 2 :(得分:0)

附加多事件处理程序至同一事件中反应的

也许,你在同一个目标上设置多点击处理程序!

https://gist.github.com/xgqfrms-GitHub/a36b56ac3c0b4a7fe948f2defccf95ea#gistcomment-2136607

jsx

<div style={{ display: 'flex' }}>
    <div style={{
            width: '270px',
            background: '#f0f0f0',
            borderRight: "30px solid red",
            minHeight: ' 500px',
            maxHeight: '700px',
            overflowX: 'hidden',
            overflowY: 'scroll',
        }}
        onClick={this.state.ClickHandler}
        onClick={this.stateHandleClick}
        className="sidebar-btn"
        >
        <button onClick={this.props.ClickHandler}>props</button>
        <button onClick={(e) => this.props.ClickHandler}>props</button>
        <button onClick={this.props.ClickHandler}>props</button>
        <button onClick={this.state.ClickHandler}>state</button>
 //...
</div>

答案 3 :(得分:-1)

对事件分组多个操作

onMouseDown={(e) => { e.stopPropagation(); alert('hello'); }}
相关问题