Created
October 26, 2017 12:28
-
-
Save mpolinowski/be444c52280598752a408b9e9b3de123 to your computer and use it in GitHub Desktop.
React — Dynamic Component Names with JSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> | |
// } |
it almost works, but in my case instead of rendering
it renders
which renders Html instead of the component
any idea?
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
alternative doesnt work, but the first one worked great! thanks man :)