javascript - 如何从父组件在React中的子组件中设置动态组件?

标签 javascript node.js react-native reactjs

我有一个父组件,它拉入一个子组件,该子组件又拉入另一个子组件。我希望能够设置该 child 的子组件来自顶级父级。不知道该怎么做。这是一些代码来演示我想要做什么:

var TopParent = React.createClass({
    render: function() {
        return (
            <div className="topParent">
                <Child componentVariable="BottomChild">
            </div>
        );
    }
});

var Child = React.createClass({
    render: function() {
        return (
            <div className="child">
                <{this.props.componentVariable} />  // this should pull in a component based on what is passed from TopParent
            </div>
        );
    }
});

var BottomChild = React.createClass({
    render: function() {
        return (
            <div className="bottomChild">
                I am the bottom child. I should be able to be swapped out from TopParent.
            </div>
        );
    }
});

此外,一旦我弄清楚如何执行此操作,如何确保 Child 需要 BottomChild 组件的正确文件?

最佳答案

只需使用实际引用,而不是字符串;毕竟,当您渲染像 <Child /> 这样的组件时手动,也是一个引用。

var TopParent = React.createClass({
    render: function() {
        return (
            <div className="topParent">
                <Child componentVariable={BottomChild} />
            </div>
        );
    }
});

var Child = React.createClass({
    render: function() {
        var Component = this.props.componentVariable; // make sure the var is capitalized
        return (
            <div className="child">
                <Component />
            </div>
        );
    }
});

var BottomChild = React.createClass({
    render: function() {
        return (
            <div className="bottomChild">
                I am the bottom child. I should be able to be swapped out from TopParent.
            </div>
        );
    }
});

但是,在许多情况下,允许组件 to completely control the contents of the child 是有意义的:

var TopParent = React.createClass({
    render: function() {
        return (
            <div className="topParent">
                <Child>
                    <BottomChild />
                </Child>
            </div>
        );
    }
});

var Child = React.createClass({
    render: function() {
        // `this.props.children` is the *contents* of the `Child` component
        // as specified in the JSX of `TopParent`
        return (
            <div className="child">
                {this.props.children}
            </div>
        );
    }
});

var BottomChild = React.createClass({
    render: function() {
        return (
            <div className="bottomChild">
                I am the bottom child. I should be able to be swapped out from TopParent.
            </div>
        );
    }
});

关于javascript - 如何从父组件在React中的子组件中设置动态组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33113261/

相关文章:

javascript - 通过Javascript在另一个html页面中显示HTML表单

html - Node.js HTML 和 CSS

node.js - Node and Express - 路由特定资源的 Assets 管理器

javascript - react 原生 : How to make change in specific item of ScrollView

javascript - Netsuite 计划脚本保存的搜索

javascript - 无法使用 jQuery .select2 选择项目

node.js - npm 在 Windows 上安装 EACCES 错误

reactjs - 连接 redux 功能不适用于 react native

mysql - React Native 获取 MYSQL 中的数据

javascript - 无法在自定义指令中调用jquery datetimepicker函数