How to add multiple event handlers to same event in React.js

后端 未结 4 1170
星月不相逢
星月不相逢 2021-02-13 19:35

All:

I wonder if it is possible that binding multiple event handlers to same event?

For example:

var LikeToggleButton = React.createClass({
    r         


        
4条回答
  •  后悔当初
    2021-02-13 20:12

    If you want to have multiple callbacks executed when onClick is triggered, you can have them passed from outside, so you'll have access to them in the props object. Then execute them all (note: code not tested):

    var LikeToggleButton = React.createClass({
    
        toggle: function() {
            this.setState({liked:!like});
        },
    
        handleClick: function(e) {
            e.preventDefault();
            this.toggle();
            for (var i=0, lTOGGLE LIKE
    ); } });

    BUT, if you want to have components connected between them, you should not do that by calling methods inside handlers. Instead you should use an architectural pattern, where Flux is the obvious choice (but there are lots more).

    Take a look to Flux, and here you have more choices.

提交回复
热议问题