React render component using its name

こ雲淡風輕ζ 提交于 2019-12-04 00:02:49
Brigand

You were close. You need to pass the component class it self (not a string), and then because the tag syntax takes a variable already, you get rid of the {}s. Also don't forget to return the node from render.

var Container = React.createClass({
    render: function() {
        return <this.props.implComponent />
    }
});

React.render(
  <Container implComponent={SomePredefinedVariable} />,
   document.getElementById('content')
);

If you want to pass a basic dom component, you'd use a string

This makes sense if you think about the transform result

var Container = React.createClass({displayName: "Container",
    render: function() {
      return React.createElement(this.props.implComponent, null)
    }
});

ReactDOM.render(
  React.createElement(Container, {implComponent: SomePredefinedVariable}),
   document.getElementById('content')
);
ReactDOM.render(
  React.createElement(Container, {implComponent: "div"}),
   document.getElementById('content')
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!