Skip to content

Instantly share code, notes, and snippets.

@mhmdAljefri
Last active October 29, 2018 07:04
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 mhmdAljefri/3b6760026049821b45180289d594ccb4 to your computer and use it in GitHub Desktop.
Save mhmdAljefri/3b6760026049821b45180289d594ccb4 to your computer and use it in GitHub Desktop.
import React from 'react'
const codeSplittingHoc = impoertComponent =>
class componentName extends React.Component {
state = {
component: null // default state for component
}
async componentDidMount() {
const { default: component } = await impoertComponent(); // import default from function and assign it to component
this.setState({ component }) // set component module component as state to use it on render()
};
render() {
const { component: Component } = this.state; // Capitlize component to use it as React Component
return (
Component ?
<Component /> :
null {* do what you want @here *}
)
}
}
export default codeSplittingHoc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment