Created
December 4, 2017 01:47
-
-
Save dance2die/95259efdf4f883f1ceae62ca1346734a to your computer and use it in GitHub Desktop.
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, { Component } from "react"; | |
import shortid from "shortid"; | |
import "./App.css"; | |
class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
components: [] | |
}; | |
} | |
addComponent = async type => { | |
console.log(`Loading ${type} component...`); | |
import(`./components/${type}.js`) | |
.then(component => | |
this.setState({ | |
components: this.state.components.concat(component.default) | |
}) | |
) | |
.catch(error => { | |
console.error(`"${type}" not yet supported`); | |
}); | |
}; | |
async componentDidMount() { | |
const { events } = this.props; | |
events.map(async type => await this.addComponent(type)); | |
} | |
render() { | |
const { components } = this.state; | |
if (components.length === 0) return <div>Loading...</div>; | |
const componentsElements = components.map(Component => ( | |
<Component key={shortid.generate()} /> | |
)); | |
return <div className="App">{componentsElements}</div>; | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment