-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dalurness/du/dynamicSolution
fix dynamically rendered component
- Loading branch information
Showing
2 changed files
with
10 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { lazy, Suspense } from "react"; | ||
|
||
// this component allows us to dynamically import components for question results that are unique to each specific question. | ||
|
||
// https://vite.dev/guide/features.html#glob-import | ||
let x: Record<string, astroHTML.JSX.Element> = import.meta.glob('./solutions/*', { eager: true, import: "default" }) | ||
export function DynamicSolution({ resultComponent }: { resultComponent: string }) { | ||
// btw...be aware of footguns when engaging in jank | ||
// See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats | ||
const Component = lazy(() => import(/* @vite-ignore */`./solutions/${resultComponent}`)); | ||
let path = `./solutions/${resultComponent}.tsx` | ||
let Component = x[path] | ||
|
||
if (!Component) return null | ||
|
||
return ( | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Component /> | ||
</Suspense> | ||
); | ||
<Component client:load /> | ||
) | ||
}; |
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