Skip to content

Instantly share code, notes, and snippets.

@hugows
Forked from magbicaleman/index.js
Created May 6, 2020 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugows/fe0bae07b460332ee51cbdd9927c84cd to your computer and use it in GitHub Desktop.
Save hugows/fe0bae07b460332ee51cbdd9927c84cd to your computer and use it in GitHub Desktop.
Dynamic imports are very interesting..
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import registerServiceWorker from './registerServiceWorker';
class Dynamic extends Component {
constructor(props) {
super(props);
this.state = { module: null };
}
componentDidMount() {
const { path } = this.props;
import(`${path}`)
.then(module => this.setState({ module: module.default }))
}
render() {
const { module: Component } = this.state; // Assigning to new variable names @see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
return(
<div>
{Component && <Component />}
</div>
)
}
}
ReactDOM.render(<Dynamic path='./App' />, document.getElementById('root'));
registerServiceWorker();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment