3

I'd like to create a super general component that can be built with any element type. ie, I'd like to do something like this:

const Wrapper = React.createClass({
  render() {
    const { props } = this;
    const elementType = props.elementType;
    return (
      <{elementType} className={className}>
        {props.children}
      </{elementType}>
    );
  }
});

but of course React gets very angry when I attempt this. Is there any way to build a component with to-be-decided HTML types?

EFH
  • 431
  • 1
  • 6
  • 14

4 Answers4

3

You can use React.createElement to accomplish that (after all, JSX just provides syntactic sugar for it).

Something like that:

const myElement = React.createElement(

   /* type */
    props.elementType, //must be a string or a React Component class

    /* props object */
    { 
        className: className
    },

    /* You can pass children to the third parameter also. */
    props.children
);

return myElement;
mrlew
  • 7,078
  • 3
  • 25
  • 28
3

This depends very much on what elementType is. In JSX, you can either pass in a custom component itself, or a string (e.g. h1, p) as a "tag". In any case, you don't want to put the curly brackets {} around the tag. Very simple example:

// Usually you want to import this. (This is just a stateless component)
const Element = (props) => <em>{props.children}</em>;

class Example extends React.Component {
  constructor() {
    super();
    this.state = { element1: 'h1', element2: Element };
  }
  render() {
    return(
      <div>
        <this.state.element1>Hello!</this.state.element1>
        <this.state.element2>World!</this.state.element2>
      </div>
    );
  }
}

ReactDOM.render(<Example />, document.getElementById('View'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='View'></div>

If you want to use a string as a reference to a custom component, you will need to use native JavaScript with React's createElement/cloneElement.

Fabian Schultz
  • 18,138
  • 5
  • 49
  • 56
3

Yes, you can — even with JSX if you'd like.

The trick is to understand how JSX is translated into the React.createElement calls:

  • bracketed text starting with a lowercase character is translated into a string: <div/> becomes React.createElement('div').
  • bracketed text starting with an uppercase is assumed to be a variable in scope: <SomeComponent/> becomes React.createElement(SomeComponent).

Now imagine if you assign var SomeComponent = 'div'. Then the uppercase rule would apply (passing the variable to createElement), but it will have the same effect as the first rule (because the variable references a string)!

Here's how it can look in a practical situation:

let Child = ({EL,thing}) => <EL className="helper-thing">{{ thing.name }}</EL>


let ListParent = ({things}) => <ul>
  {things.map(t => <Child EL='li' thing={t}/>)}
</ul>

let ArticleParent = ({things}) => <article>
  {things.map(t => <Child EL='p' thing={t}/>)}
</article>

See the official docs on Choosing the Type at Runtime for a slightly different — perhaps cleaner — approach that capitalizes the property inside the child component.

natevw
  • 16,807
  • 8
  • 66
  • 90
0

JSX does not allow this, but you can do it in pure Javascript React. You can create the component:

const element = React.createElement(elementType, {className}, props.children);

then return element from your render method. In fact, this is more or less what JSX would compile to, if it allowed you to do what you're proposing.

See https://facebook.github.io/react/docs/react-without-jsx.html for more discussion.

Joshua Engel
  • 495
  • 3
  • 18