Skip to content

Instantly share code, notes, and snippets.

@matmalkowski
Created March 28, 2019 13:44
Show Gist options
  • Save matmalkowski/d9d124ba1951e482f2523c60a4b574a1 to your computer and use it in GitHub Desktop.
Save matmalkowski/d9d124ba1951e482f2523c60a4b574a1 to your computer and use it in GitHub Desktop.
import * as React from 'react'
const useMDXFiles = (rolesMDXs: any) => {
const [mods, setMods] = React.useState<any>([])
const importModule = (pathname: string) => rolesMDXs[pathname]()
const allPaths = Object.keys(rolesMDXs)
const loader = allPaths.map(async (pathname: string) => {
const { default: MDXComponent, ...other } = await importModule(pathname)
return { MDXComponent, ...other }
})
const loadMDXModules = async () => {
const modules = await loader
Promise.all(modules).then(e => {
setMods(e)
})
}
React.useEffect(() => {
loadMDXModules()
}, [])
return mods
}
export default useMDXFiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment