动态渲染React组件


78

在React JSX中,似乎无法执行以下操作:

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

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

我正在设计此组件,以便在内部隐藏的值this.props.component.slug将包含有效的HTML元素(h1,p等)。有什么办法可以使这项工作吗?

Answers:


96

您不应该将组件子弹放在花括号中:

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);

这是一个有效的小提琴:http : //jsfiddle.net/kb3gN/6668/

另外,您还可以找到JSX编译器,有助于调试这些类型的错误:http : //facebook.github.io/react/jsx-compiler.html


16
我刚刚解决的一个难题是,您必须提供一个点以使任何其他变量都可以被识别,var page; <page></page>即将无法工作,而var page = { component: component }; <page.component></page.component>可以
markyzm 2016年

5
如果变量使用大写字母或带有点符号,则React会将其视为自定义元素,否则将其视为HTML元素。即<页> </页>工程太
bebbi

必须使用'div'替换React.DOM.div才能正常工作
galki

3
请记住,变量应包含组件本身,不仅仅是字符串的组件名称。
totymedli

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'));

这是一个有效的小提琴:https : //jsfiddle.net/janklimo/yc3qcd0u/


你摇滚!变量名应以大写字母开头。这很奇怪,但事实就是如此。
Aftab Naveed

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"));
});

4

编辑:也许您忘记添加/** @jsx React.DOM */在js开头?

您可以使用React.DOM

render: function() {
  return React.DOM[this.props.component.slug](null, this.props.component.value);
}

http://jsbin.com/rerehutena/2/edit?html,js,输出

我不是React专家,但我认为每个组件的开头都应使用特定的标签构建。因此,它本身可以提出一个明确的目的。


0

对我来说,解决方案是将导入的Component分配给一个变量(使用CapitalCase),然后呈现该变量。

例:

import React, { Component } from 'react';
import FooComponent from './foo-component';
import BarComponent from './bar-component';

class MyComponent extends Component {
    components = {
        foo: FooComponent,
        bar: BarComponent
    };

        //this is the most important step
       const TagName = this.components.foo;

    render() {
       return <TagName />
    }
}
export default MyComponent;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.