简体   繁体   English

反应:从非所有者父组件中调用函数

[英]React: Call a function from a non-owner parent component

I know you can pass event handlers into children that are explicitly rendered by an owner component, like this: 我知道您可以将事件处理程序传递给由所有者组件显式呈现的子代,如下所示:

var Owner = React.createClass({
    eventHandler: function() {
        console.log("Interaction!");
    },
    render: function() {
        return (
            <div>
                <input onChange={this.eventHandler}/>
                <button onClick={this.eventHandler}/>
            </div>
        );
    }
});

But what if I wanted to pass these functions from a parent component to its nested this.props.children ? 但是,如果我想将这些功能从父组件传递到嵌套的this.props.children呢?

var Parent = React.createClass({
    eventHandler: function() {
        console.log("Interaction!");
    },
    render: function() {
        return (
            <span>
                {this.props.children}
            </span>
        );
    }
});
var Owner = React.createClass({
    render: function() {
        return (
            <div>
                <Parent>
                    <input onChange={whatDoIPutHere}/>
                    <button onClick={orDoIPutThisInTheParentIDK}/>
                </Parent>
             </div>
         );
});

I got something working by doing this: 通过这样做,我得到了一些工作:

var Parent = React.createClass({
    eventHandler: function() {
        console.log("Interaction!");
    },
    render: function() {
        var newChildren = this.props.children.map(child => {
            var newProps = {};
            if (child.type === 'input') {
                newProps.onChange = this.eventHandler;
            } else if (child.type === 'button') {
                newProps.onClick = this.eventHandler;
            }
            return React.cloneElement(child, newProps);
        });
        return (
            <span>
                {newChildren}
            </span>
        );
    }
});

But that requires me to hard-code which element types to look for, and which prop to pass eventHandler to. 但这需要我硬编码要查找的元素类型,以及将eventHandler传递给的道具。 I want to be able to specify all that in the <Owner> component, and have <Parent> just be a generic container that's agnostic to how exactly its eventHandler gets called. 我希望能够在<Owner>组件中指定所有内容,并让<Parent>只是一个通用容器,而该容器不知道其eventHandler的调用方式。

Is there any way to do this? 有什么办法吗? I'd like to avoid any workarounds like getDOMNode() , because I'm expecting this codebase might be shared with React Native in the future. 我想避免像getDOMNode()这样的变通办法,因为我希望这个代码库将来可以与React Native共享。

Why not just generalize the different interaction methods, and just pass the type of interaction back up to the handler? 为什么不仅仅概括不同的交互方法,而只是将交互类型传递回处理程序? Something like: 就像是:

var cloned = React.Children.map(this.props.children, function (c) {
    return React.cloneElement(c, {
      onChange: this.props.onInteraction.bind(this, 'change'),
      onClick: this.props.onInteraction.bind(this, 'click')
    });
}, this);

That way you get notified of all clicks and changes (other types of interaction could be added to the list, too, like onKeyUp , onMouseMove , etc) 这样一来,您将获得所有点击和更改的通知(其他类型的互动方式也可以添加到列表中,例如onKeyUponMouseMove等)

Link to jsbin example 链接到jsbin示例

Based on rossipedia's answer, I figured this out: 根据rossipedia的回答,我知道了这一点:

var Parent = React.createClass({
    eventHandler: function() {
        console.log("Interaction!");
    },
    render: function() {
        var newChildren = React.Children.map(this.props.children, child => {
            var newProps = {};
            for (var i in child.props) {
                if (child.props[i] === "onInteraction") {
                    newProps[i] = this.eventHandler;
                }
            }
            return React.cloneElement(child, newProps);
        });
        return (
            <span>
                {newChildren}
            </span>
        );
    }
});

which allows me to do this: 这使我可以这样做:

<Parent>
    <input onChange="onInteraction"/>
</Parent>

Not super elegant, but it works. 不是超级优雅,但可以。 Might be able to figure out something better with parent-based context once React 0.14 hits. 一旦React 0.14命中,就可以通过基于父级的上下文找出更好的方法。

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

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