Last active
March 11, 2018 13:40
-
-
Save dave-nicholas/1480219bede6a0523e67da7339998b2b to your computer and use it in GitHub Desktop.
A composed HOC for code splitting in react (requires recompose)
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 React from 'react'; | |
import { withState, compose, lifecycle } from 'recompose'; | |
export default componentToImport => { | |
const state = withState('component', 'setComponent', null); | |
const hooks = lifecycle({ | |
async componentDidMount() { | |
const { default: C } = await componentToImport(); | |
this.props.setComponent(() => props => <C {...props} />); | |
} | |
}); | |
const AsyncComponent = ({ component: C, ...props }) => | |
C ? <C {...props} /> : null; | |
const AsyncContainer = compose(state, hooks)(AsyncComponent); | |
return AsyncContainer; | |
}; |
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 AsyncContainer from './AsyncContainer'; | |
const AsyncSomething = AsyncContainer(() => import('./somecomponent')); | |
/* ............ */ | |
<Route component={AsyncSomething} path='/something' /> | |
/* ............ */ | |
<AsyncSomething /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment