Skip to content

Instantly share code, notes, and snippets.

@juice49
Created August 28, 2019 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juice49/2b2acd327d6aa6abcfa515ee5677f2c7 to your computer and use it in GitHub Desktop.
Save juice49/2b2acd327d6aa6abcfa515ee5677f2c7 to your computer and use it in GitHub Desktop.
useLazyModule
// Sadly a full dynamic import is not compatible
// with webpack's `import()`.
import { useState } from 'react'
const useLazyModule = path => {
const [ module, setModule ] = useState()
;(async () => {
const module = await import(path)
setModule(module)
})()
return module
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment