Created
April 15, 2018 00:43
-
-
Save cyan33/09b737f2c52d694792c8c79c32bf30f1 to your computer and use it in GitHub Desktop.
async react component for code splitting
This file contains hidden or 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"; | |
export default function asyncComponent(importComponent) { | |
class AsyncComponent extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
component: null | |
}; | |
} | |
async componentDidMount() { | |
const { default: component } = await importComponent(); | |
this.setState({ | |
component: component | |
}); | |
} | |
render() { | |
const C = this.state.component; | |
return C ? <C {...this.props} /> : null; | |
} | |
} | |
return AsyncComponent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment