I have a React component defined in JSX that returns a cell using either td or th , for example:
if(myType === 'header') { return ( <th {...myProps}> <div className="some-class">some content</div> </th> ); } return ( <td {...myProps}> <div className="some-class">some content</div> </td> );
Is it possible to write JSX in such a way that the HTML tag is taken from a variable? How:
let myTag = myType === "header" ? 'th' : 'td'; return ( <{myTag} {...myProps}> <div className="some-class">some content</div> </{myTag}> );
The above code returns an unexpected token error indicating { . I am using webpack with the babel plugin to compile JSX.
source share