Last active
October 13, 2018 15:19
-
-
Save xeqlol/1032a192fb278b27a304a6f8eff0b6a8 to your computer and use it in GitHub Desktop.
react + redux + saga code splitting mind experiment
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 { wrapModule } from './loader'; | |
| const About = ... // component, connected to store; | |
| const aboutReducer = ... // root reducer | |
| const aboutSaga = ... // root saga | |
| export default wrapModule(About, aboutReducer, aboutSaga); |
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 { wrapModule } from './loader'; | |
| const Home = ... // component, connected to store; | |
| const homeReducer = ... // root reducer | |
| const homeSaga = ... // root saga | |
| export default wrapModule(Home, homeReducer, homeSaga); |
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 { store, sagaMiddleware } from 'somewhere where store and sagaMiddleware were initialized' | |
| export function createLoadableComponent(store, sagaMiddleware) { | |
| // that looks awful, needs some research | |
| const loadedModules = {}; | |
| const loadedReducers = {}; | |
| const loadedComponents = {}; | |
| return (componentName, loader) => { | |
| return class LoadableComponent extends React.Component { | |
| state = { componentLoaded: false, component: null }; | |
| componentWillMount() { | |
| if (loadedModules[componentName]) { | |
| this.setState({ | |
| componentLoaded: true, | |
| component: loadedComponents[componentName] | |
| }); | |
| return; | |
| } | |
| loader().then(module => { | |
| console.log(`module ${componentName} loaded`); | |
| const { container, reducer, saga } = module.default; | |
| loadedModules[componentName] = true; | |
| loadedComponents[componentName] = container; | |
| loadedReducers[componentName] = reducer; | |
| store.replaceReducer(combineReducers(loadedReducers)); | |
| sagaMiddleware.run(saga); | |
| this.setState({ componentLoaded: true, component: container }); | |
| }); | |
| } | |
| render() { | |
| if (!this.state.componentLoaded) { | |
| return <div>loading</div>; | |
| } | |
| const Component = this.state.component; | |
| return <Component />; | |
| } | |
| }; | |
| }; | |
| } | |
| export const loadable = createLoadableDecorator(store, sagaMiddleware); | |
| export function wrapModule(container, reducer, saga) { | |
| return { | |
| container, | |
| reducer, | |
| saga | |
| }; | |
| } |
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 { loadable } from './loader' | |
| export const Home = loadable('Home', () => import('./HomeContainer')); | |
| export const About = loadable('About', () => import('./AboutContainer')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment