Skip to content

Instantly share code, notes, and snippets.

@mpolinowski
Created October 26, 2017 12:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpolinowski/be444c52280598752a408b9e9b3de123 to your computer and use it in GitHub Desktop.
Save mpolinowski/be444c52280598752a408b9e9b3de123 to your computer and use it in GitHub Desktop.
React — Dynamic Component Names with JSX
import React from 'react'
import Layout from '../components/layout'
import Products from '../components/_products'
import IndoorCameras from '../components/_indoorCameras'
import OutdoorCameras from '../components/_outdoorCameras'
class InstarProducts extends Component {
components = {
products: Products,
indoorCameras: IndoorCameras,
outdoorCameras: OutdoorCameras,
};
render() {
const TagName = this.components[this.props.tag || 'products'];
return <TagName />
}
}
export default InstarProducts;
// ############### imported with tag as prop
//
// import React, { Component } from 'react';
// import MyComponent from './my-component';
//
// class App extends Component {
// render() {
// return (
// <div>
// <InstarProducts tag="products" />
// <InstarProducts tag="OutdoorCameras" />
// </div>
// );
// }
// }
// export default App;
// alternative
//
// render() {
// const TagName = this.props.tag;
// return <TagName />
// }
@ismailbayram
Copy link

alternative doesnt work, but the first one worked great! thanks man :)

@alexadark
Copy link

it almost works, but in my case instead of rendering
it renders
which renders Html instead of the component
any idea?

@Programador02SM
Copy link

The component load like 'Undefined',
when i'm trying to return from a const function out of the componente class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment