动态渲染React组件

eri*_*per 78 javascript reactjs

在React JSX中,似乎不可能做这样的事情:

render: function() {
  return (
    <{this.props.component.slug} className='text'>
      {this.props.component.value}
    </{this.props.component.slug}>
  );
}
Run Code Online (Sandbox Code Playgroud)

我得到一个解析错误:意外的令牌{.这不是React可以处理的吗?

我正在设计这个组件,以便在引擎盖下,存储的值this.props.component.slug将包含有效的HTML元素(h1,p等).有没有办法让这项工作?

nil*_*gun 93

你不应该将组件slug放在花括号中:

var Hello = React.createClass({
    render: function() {
        return <this.props.component.slug className='text'>
            {this.props.component.value}
        </this.props.component.slug>;
    }
});

React.renderComponent(<Hello component={{slug:React.DOM.div, value:'This is my header'}} />, document.body);
Run Code Online (Sandbox Code Playgroud)

这是一个工作小提琴:http://jsfiddle.net/kb3gN/6668/

此外,您可以找到JSX Compiler有助于调试这些错误:http: //facebook.github.io/react/jsx-compiler.html

  • 我刚刚解决的一个问题是你必须提供一个点才能识别任何其他变量,即`var page; <page> </ page>`不起作用,而`var page = {component:component}; <page.component> </ page.component>`的确如此 (16认同)
  • 反应对待变量和自定义元素,如果他们的资本或有一个点符号,其他HTML元素.即<页> </页>工程太 (5认同)
  • 请记住,**变量应该保持组件**本身而不是**只是组件的名称****字符串**. (3认同)

Jan*_*imo 22

正如nilgun之前指出的那样,组件段塞不应该用花括号包裹.

如果您决定将其存储在变量中,请确保以大写字母开头.

这是一个例子:

var Home = React.createClass({
  render: function() {
    return (
      <div>
        <h3>This is an input</h3>
        <CustomComponent inputType="input" />
        <h3>This is a text area</h3>
        <CustomComponent inputType="textarea" />
      </div>
    );
  }
});

var CustomComponent = React.createClass({
  render: function() {
    // make sure this var starts with a capital letter
    var InputType = this.props.inputType;
    return <InputType />;
  }
});

React.render(<Home />, document.getElementById('container'));
Run Code Online (Sandbox Code Playgroud)

这是一个工作小提琴:https://jsfiddle.net/janklimo/yc3qcd0u/


Gud*_*son 5

如果您打算注入渲染的实际组件,则可以执行类似的操作,这对于测试非常方便,或者您希望动态注入要渲染的组件的任何原因。

var MyComponentF=function(ChildComponent){
    var MyComponent = React.createClass({
        getInitialState: function () {
            return {
            };
        },
        componentDidMount: function () {
        },
        render: function () {
            return (
                <div className="MyComponent">
                    <ChildComponent></ChildComponent>
                </div>
            );
        }
    });
    return MyComponent;
};

var OtherComponentF=function(){
    var OtherComponent = React.createClass({
        getInitialState: function () {
            return {
            };
        },
        componentDidMount: function () {
        },
        render: function () {
            return (
                <div className="OtherComponent">
                    OtherComponent
                </div>
            );
        }
    });
    return OtherComponent;
};

var AnotherComponentF=function(){
    var AnotherComponent = React.createClass({
        getInitialState: function () {
            return {
            };
        },
        componentDidMount: function () {
        },
        render: function () {
            return (
                <div className="AnotherComponent">
                    AnotherComponent
                </div>
            );
        }
    });
    return AnotherComponent;
};

$(document).ready(function () {
    var appComponent = MyComponentF(OtherComponentF());

    // OR
    var appComponent = MyComponentF(AnotherComponentF());

    // Results will differ depending on injected component.

    ReactDOM.render(React.createElement(appComponent), document.getElementById("app-container"));
});
Run Code Online (Sandbox Code Playgroud)