Last active
April 25, 2023 09:06
-
-
Save gaearon/fbd581089255cd529e62 to your computer and use it in GitHub Desktop.
Webpack's async code splitting with React Router
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
'use strict'; | |
var React = require('react'); | |
function createAsyncHandler(getHandlerAsync, displayName) { | |
var Handler = null; | |
return React.createClass({ | |
displayName: displayName, | |
statics: { | |
willTransitionTo(transition, params, query, callback) { | |
getHandlerAsync().then(resolvedHandler => { | |
Handler = resolvedHandler; | |
if (!Handler.willTransitionTo) { | |
return callback(); | |
} | |
Handler.willTransitionTo(transition, params, query, callback); | |
if (Handler.willTransitionTo.length < 4) { | |
callback(); | |
} | |
}); | |
}, | |
willTransitionFrom(transition, component, callback) { | |
if (!Handler || !Handler.willTransitionFrom) { | |
callback(); | |
} | |
Handler.willTransitionFrom(transition, component, callback); | |
if (Handler.willTransitionFrom.length < 3) { | |
callback(); | |
} | |
} | |
}, | |
render() { | |
return <Handler {...this.props} />; | |
} | |
}); | |
} | |
module.exports = createAsyncHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just researched a bunch of this, and settled on https://github.com/luqin/react-router-loader. It has considered several other forks of loaders and appears to be maintained.
Here's a gist of the dynamic auth-flow example using jsx syntax:
https://gist.github.com/rosskevin/de55079e0d48f046fb518e45bb4f12b1