Created
July 27, 2017 14:04
-
-
Save dmueller39/04b01335c87ab28665812c6807d7462a to your computer and use it in GitHub Desktop.
Lazy Higher Order Component
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
// @flow | |
import React, { PureComponent } from 'react'; | |
export default function createLazyComponent<A>( | |
getClass: () => Class<React.Component<void, A, void>> | |
) { | |
return class extends PureComponent { | |
componentWillMount() { | |
this.lazyComponentClass = getClass(); | |
} | |
lazyComponentClass: ?Class<React.Component<void, A, void>>; | |
render() { | |
const LazyComponent = this.lazyComponentClass; | |
if (LazyComponent == null) { | |
return null; | |
} | |
return ( | |
<LazyComponent {...this.props}> | |
{this.props.children} | |
</LazyComponent> | |
); | |
} | |
}; | |
} |
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 createLazyComponent from './LazyComponent'; | |
const LazyExpensiveComponent = createLazyComponent(() => require('./ExpensiveComponent')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment