Instantly share code, notes, and snippets.

Embed
What would you like to do?
addView = async viewName => {
// Don't load more than once.
if (this.state.loadedComponents.includes(viewName)) return;
console.log(`Loading ${viewName} view...`);
import(`./views/${viewName}.js`)
.then(Component => {
this.setState({
loadedComponents: this.state.loadedComponents.concat(viewName),
components: this.state.components.concat(
<Component.default
key={shortid.generate()}
data={this.props.data}
/>
)
});
})
.catch(error => {
this.setState({
loadedComponents: this.state.loadedComponents.concat(viewName),
components: this.state.components.concat(
<NullView key={shortid.generate()} />
)
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment