patternjavascriptreactCritical
Dynamic tag name in React JSX
Viewed 0 times
jsxtagdynamicreactname
Problem
I am trying to write a React component for HTML heading tags (
I tried to do it like this:
And I expected output like:
But this is not working.
Is there any way to do this?
h1, h2, h3, etc.), where the heading level is specified via a prop.I tried to do it like this:
HelloAnd I expected output like:
HelloBut this is not working.
Is there any way to do this?
Solution
No way to do that in-place, just put it in a variable (with first letter capitalised):
const CustomTag = `h${this.props.level}`;
HelloCode Snippets
const CustomTag = `h${this.props.level}`;
<CustomTag>Hello</CustomTag>Context
Stack Overflow Q#33471880, score: 595
Revisions (0)
No revisions yet.