Reactjs 在React 0.12中动态渲染React组件
,但使用React 0.12及其带来的JSX更改,我现在正在寻找实现此功能的另一种方法 以下是在React 0.11中工作的代码:Reactjs 在React 0.12中动态渲染React组件,reactjs,react-jsx,Reactjs,React Jsx,,但使用React 0.12及其带来的JSX更改,我现在正在寻找实现此功能的另一种方法 以下是在React 0.11中工作的代码: return React.createClass({ render: function() { var Tag = React.DOM[this.props.element]; return ( <Tag className='text'> {this.props.value}
return React.createClass({
render: function() {
var Tag = React.DOM[this.props.element];
return (
<Tag className='text'>
{this.props.value}
</Tag>
);
}
});
需要一些指导来帮助解释该警告,我找不到关于如何执行类似操作的好文档。对此的回答非常简单(+1表示React 0.12,使事情更简单!),但需要做的只是删除变量定义的React.DOM[]部分,传递字符串文字:
return React.createClass({
render: function() {
var Tag = this.props.element;
return (
<Tag className='text'>
{this.props.value}
</Tag>
);
}
});
return React.createClass({
render:function(){
var Tag=this.props.element;
返回(
{this.props.value}
);
}
});
作品优美;没有任何控制台警告 另外,您还可以传递自定义组件,例如MyComponent,而不是“p”或“h1”。术语方面,react中的元素是
(编译为react.createElement(Something)`)的结果,'component'或'type'或更特定于用例的东西可能是更清晰的道具名称。!重要!标记名必须大写,才能正常工作。
return React.createClass({
render: function() {
var Tag = this.props.element;
return (
<Tag className='text'>
{this.props.value}
</Tag>
);
}
});