Basically it comes down to pre-creating all the pieces you will ever need. Then you just need a way to dynamically reference them. Here's a solution based on mine:
http://henleyedition.com/implicit-code-splitting-with-react-router-and-webpack
and here is what I am doing as I am not using React Router (side note: I don't think this is a good match for reduction or animation):
//loader: { test: (folder)\/.*\.js, include: path.resolve(__dirname, 'src') loader: ['lazy?bundle', 'babel'] } //dynamic usage within React component: const My_COMPONENTS = { ComponentA: require('./folder/ComponentA'), ComponentB: require('./folder/ComponentB'), } class ParentComponent extends React.Component { componentDidMount() { My_COMPONENTS[this.props.name](component => this.setState({component})); } render() { return <this.state.component />; } }
So, the result is that you are dynamically representing the component, but from the static predefined set of features - all the time, only sending more to the client than the pieces that the visitor is actually interested in.
ALSO, here is the component that I have:
import React from 'react'; import Modal from './widgets/Modal'; export default class AjaxModal extends React.Component { constructor(props, context) { super(props, context); this.state = { Content: null }; } componentDidMount() { if(this.props.show) { this.loadContent(); } } componentWillReceiveProps({show}) { if(show && !this.state.Content) { this.loadContent(1200);
pass pass async so that the bundler function runs like this.props.requestLazybundle , for example:
render() { let requestLazyBundle = require('bundle?lazy&name=AnotherComponent!../content/AnotherComponent'); return ( <AjaxModal title='Component Name' {...props} requestLazyBundle={requestLazyBundle} /> ); }
faceyspacey.com
source share