React - how to add attributes to dynamic tag name

0 points
Created by:

In this article, we would like to show you how to add attributes to dynamic tag name in React.

Below we create three tags using dynamic tag names and pass different style attributes to components with different priority attributes.


In this article, we describe how to create dynamic tag names.

Runnable example:

// ONLINE-RUNNER:browser;

// Note: Uncomment import lines while working with JSX Compiler.
// import React from 'react';
// import ReactDOM from 'react-dom';

// style objects -------------------------

const style1 = {
  color: 'orange',

const style2 = {
  color: 'lightgreen',

const style3 = {
  color: 'blue',

// dynamic tag name -------------------------

const MyComponent = (props) => {
  const CustomTag = `h${props.priority}`;
  return <CustomTag style={}>priority = {props.priority}</CustomTag>;

// adding style attribute ------------------------- 

const App = () => {
  return (
      <MyComponent style={style1} priority="1" />
      <MyComponent style={style2} priority="2" />
      <MyComponent style={style3} priority="3" />

const root = document.querySelector('#root');
ReactDOM.render(<App />, root );

Related posts

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.


React - add attributes to dynamic tag name
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂