也许有点太晚了,但是您可以动态创建组件或标签,而无需使用React.createClass
JSX 将标签名称放入变量中,并像使用任何其他组件一样使用该变量。
在你的情况下, inside render
,你应该有类似的东西:
const TitleTag = `h{ this._checkedDepth }>`;
return <TitleTag>{ this.props.name }</TitleTag>
请注意,该变量的第一个字符必须为大写,以便让 React 知道这是一个 React 组件,否则将插入一个与您的变量完全相同的名称(不是值)的标签。
请参阅https://reactjs.org/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized:
当元素类型以小写字母开头时,它指代内置组件,如<div>
or<span>
并生成字符串'div'
or'span'
传递给React.createElement
. 以大写字母开头的类型,例如<Foo />
编译React.createElement(Foo)
并对应于您的 JavaScript 文件中定义或导入的组件。
我们建议使用大写字母命名组件。如果您确实有一个以小写字母开头的组件,请在 JSX 中使用它之前将其分配给大写变量。
如果不创建该变量,就无法做到这一点,因此尝试像在代码中那样动态地执行它是行不通的(编译器限制)。
这是一个完整的工作示例:
class HeaderComponent extends React.Component {
constructor(props) {
super(props);
const depth = Math.max(Math.min(parseInt(props.depth) || 1, 6), 1);
this.state = { depth };
}
onClick() {
let depth;
do {
depth = Math.floor(Math.random() * 6) + 1;
} while(depth === this.state.depth);
this.setState({ depth });
}
render() {
const Title = `h${ this.state.depth }`;
return <Title className="title" onClick={ () => this.onClick() }>{ this.props.name }</Title>;
}
}
ReactDOM.render(
<HeaderComponent name="Click Me!" depth="1"/>,
document.getElementById('app')
);
body { margin: 0; }
h1 { font-size: 4rem; }
h2 { font-size: 3.5rem; }
h3 { font-size: 3rem; }
h4 { font-size: 2.5rem; }
h5 { font-size: 2rem; }
h6 { font-size: 1.5rem; }
.title {
margin: 0;
padding: 0 .5rem;
cursor: pointer;
user-select: none;
}
.title:hover {
background: #FFA;
}
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>