本文介绍了React JSX 中的动态标签名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am trying to write a React component for HTML heading tags(h1
, h2
, h3
, etc.), where the heading level is specified via a prop.
I tried to do this like this:
<h{this.props.level}>Hello</h{this.props.level}>
and expected output like:
<h1>Hello</h1>
but this is not working. Is there any possible method to do this?
解决方案
No way to do that in-place, just put it in a variable (with first letter capitalised):
const CustomTag = `h${this.props.level}`;
<CustomTag>Hello</CustomTag>
这篇关于React JSX 中的动态标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!