Skip to content

Instantly share code, notes, and snippets.

@allpro
Created May 24, 2019 19:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allpro/3151cc9bf5a957e20732ba393c0a373f to your computer and use it in GitHub Desktop.
Save allpro/3151cc9bf5a957e20732ba393c0a373f to your computer and use it in GitHub Desktop.
Dynamic Import of a named-export
/*
* React.lazy helper for named-export components, instead of default-export
*
* @example const Comp = React.lazy(() => importNamed('./Component', 'Name'))
*
* @param {string} componentPath Path as used normally for import('path')
* @param {string} exportName The name of the export in this component
* @returns {Promise} The promise created by import()
*/
export default (componentPath, exportName) => (
import(componentPath).then(comp => ({ default: comp[exportName] }))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment