Created
August 1, 2019 01:26
-
-
Save dance2die/93bb446d110045552177025bf9f9412d to your computer and use it in GitHub Desktop.
Works on My Machine (I mean with Gatsby build)
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
import React, { useState, useEffect } from "react" | |
import { graphql, useStaticQuery } from "gatsby" | |
import Layout from "../components/Layout" | |
const importBlock = blockName => | |
import(`../components/blocks/${blockName}/index.js`).then( | |
component => component.default | |
) | |
export default () => { | |
const [components, setComponents] = useState([]) | |
const { allDirectory } = useStaticQuery(graphql` | |
{ | |
allDirectory(filter: { relativePath: { ne: "" } }) { | |
edges { | |
node { | |
relativePath | |
id | |
} | |
} | |
} | |
} | |
`) | |
useEffect(() => { | |
function loadComponents() { | |
allDirectory.edges.map(async ({ node }) => { | |
const component = await importBlock(node.relativePath) | |
setComponents(loadedComponents => loadedComponents.concat(component)) | |
}) | |
} | |
loadComponents() | |
}, [allDirectory]) | |
return ( | |
<Layout> | |
{components.map(Component => ( | |
<Component key={Component} /> | |
))} | |
</Layout> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment