最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React.js: attach event from parent to children - Stack Overflow

programmeradmin3浏览0评论

as shown in the example below, I'd like MyComponent to dynamically attach an "onClick" event to its children. The onClick event should fire alertView that should be able to call the clicked element method "getValue".

JSFiddle: /

How to do this? Thanks

var MyComponent = React.createClass({
    alertValue: function () {
        // RETRIEVE THE CHILD HERE
        alert(child.getValue());
    },
    render: function () {
        var children = React.Children.map(this.props.children, function (c, index) {
            return React.addons.cloneWithProps(c, {
                ref: 'child-' + index
            });
        });
        return (
            <div>
                {children}
            </div>
        );
    }
});

var MySubComponent = React.createClass({
    getValue: function () {
        return this.props.val;
    },
    render: function () {
        return (
            <div>{this.props.val}</div>
        );
    }
});

React.render(
    <div>
        <MyComponent>
            <MySubComponent val="1" />
            <MySubComponent val="2" />
            <MySubComponent val="3" />
        </MyComponent>
    </div>,
    document.getElementById('container')
);

as shown in the example below, I'd like MyComponent to dynamically attach an "onClick" event to its children. The onClick event should fire alertView that should be able to call the clicked element method "getValue".

JSFiddle: http://jsfiddle.net/2g638bp8/

How to do this? Thanks

var MyComponent = React.createClass({
    alertValue: function () {
        // RETRIEVE THE CHILD HERE
        alert(child.getValue());
    },
    render: function () {
        var children = React.Children.map(this.props.children, function (c, index) {
            return React.addons.cloneWithProps(c, {
                ref: 'child-' + index
            });
        });
        return (
            <div>
                {children}
            </div>
        );
    }
});

var MySubComponent = React.createClass({
    getValue: function () {
        return this.props.val;
    },
    render: function () {
        return (
            <div>{this.props.val}</div>
        );
    }
});

React.render(
    <div>
        <MyComponent>
            <MySubComponent val="1" />
            <MySubComponent val="2" />
            <MySubComponent val="3" />
        </MyComponent>
    </div>,
    document.getElementById('container')
);
Share Improve this question edited Apr 2, 2015 at 13:21 pistacchio asked Apr 2, 2015 at 12:14 pistacchiopistacchio 58.9k110 gold badges287 silver badges432 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

You can't call methods on child components in React. You can only set properties. (The child is actually a ReactElement which contains information about the class and associated properties. It is not an instance of the component you created).

So, you could think about this a slightly different way and move the onClick to the MySubComponent:

var MyComponent = React.createClass({
    onHandleGiveValue: function (value) {
        alert(value);
    },
    render: function () {
        const children = React.Children.map(this.props.children, child => React.cloneElement(child, { onGiveValue: this.onHandleGiveValue.bind(this) }));
        return (
            <div>
                {children}
            </div>
        );
    }
});

var MySubComponent = React.createClass({
    handleClick: function() {
        this.props.onGiveValue(this.props.val);
    },
    getValue: function () {
        return this.props.val;
    },
    render: function () {
        return (
            <div onClick={ this.handleClick } >{this.props.val}</div>
        );
    }
});

React.render(
    <div>
        <MyComponent>
            <MySubComponent val="1" />
            <MySubComponent val="2" />
            <MySubComponent val="3" />
        </MyComponent>
    </div>,
    document.getElementById('container')
);

By doing that, your code can pass the current value as an event to the parent component. I've created a new event from the MySubComponent class named onGiveValue. That for now just passes the value from this.props.val. But, it could of course be anything.

  1. Pass the parent callback to the subComponent, one dont need a reference for the child component.
  2. React prefers composition design pattern, So your parent component should contains those three subComponent. JS Fiddle: http://jsfiddle.net/68vt3umg/

    var MyComponent = React.createClass({
    handleChildClick: function (e, childValue) {
       alert(childValue);
    },
    render: function () {
        return (
            <div>
              <MySubComponent val="1" onSubClicked={this.handleChildClick}/>
              <MySubComponent val="2" onSubClicked={this.handleChildClick}/>
            </div>
        );
    }});
    
    var MySubComponent = React.createClass({
      getValue: function () {
        return this.props.val;
      },
      handleOnClick: function (e, value) {
        this.props.onSubClicked(e, this.props.val);
      },
      render: function () {
        return (
            <div onClick={this.handleOnClick}>{this.props.val}</div>
        );
      }
    });
    
    React.render(
      <div>
        <MyComponent />
      </div>,
      document.getElementById('container')
    );
    
发布评论

评论列表(0)

  1. 暂无评论