HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptreactCritical

Dynamic tag name in React JSX

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
jsxtagdynamicreactname

Problem

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 it like this:

Hello


And I expected output like:

Hello


But 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}`;

Hello

Code Snippets

const CustomTag = `h${this.props.level}`;

<CustomTag>Hello</CustomTag>

Context

Stack Overflow Q#33471880, score: 595

Revisions (0)

No revisions yet.