简体   繁体   English

React.js:将父级的事件附加到子级

[英]React.js: attach event from parent to children

as shown in the example below, I'd like MyComponent to dynamically attach an "onClick" event to its children. 如下例所示,我希望MyComponent动态地将“onClick”事件附加到其子节点。 The onClick event should fire alertView that should be able to call the clicked element method "getValue". onClick事件应该触发应该能够调用单击元素方法“getValue”的alertView。

JSFiddle: http://jsfiddle.net/2g638bp8/ 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')
);

You can't call methods on child components in React. 您无法在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). child实际上是一个ReactElement ,它包含有关类和相关属性的信息。它不是您创建的组件的实例)。

So, you could think about this a slightly different way and move the onClick to the MySubComponent : 因此,您可以考虑这种方式略有不同并将onClick移动到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 . 我已经从名为onGiveValueMySubComponent类创建了一个新事件。 That for now just passes the value from this.props.val . 现在只是传递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. 将父回调传递给subComponent,不需要子组件的引用。
  2. React prefers composition design pattern, So your parent component should contains those three subComponent. React更喜欢组合设计模式,所以你的父组件应该包含那三个subComponent。 JS Fiddle: http://jsfiddle.net/68vt3umg/ JS小提琴: 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') ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM