1

I need to set the HTML heading level based on props in a React component:

const Typography: React.FC<Props> = ({
  level
  children,
}) => {
  switch (semanticLevel) {
    case 1:
      return <h1>{children}</h1>;
    case 2:
      return <h2>{children}</h2>;
    case 3:
      return <h3>{children}</h3>;
  }
};

The code above works but feels messy. Is there a way to use the level prop directly when setting the HTML? So something like:

const Typography: React.FC<Props> = ({
  level
  children,
}) => {
   return <h`${level}`>{children}</`${level}`>;
};
Evanss
  • 23,390
  • 94
  • 282
  • 505
  • use `React.createElement` to create the heading element [demo](https://stackblitz.com/edit/react-cdyxym) – Yousaf May 25 '20 at 10:33

0 Answers0