Skip to content

Passing custom components after evaluate #1929

Answered by peterj35-dd
peterj35-dd asked this question in Q&A
Discussion options

You must be logged in to vote

Using the hooks approach described as the CRA demo, this works

import { evaluate } from "@mdx-js/mdx"
import { useEffect, useState } from "react"
import * as runtime from "react/jsx-runtime"

type UseMdxProps = {
  mdx: string
}

export const useMdx = ({ mdx }: UseMdxProps) => {
  // Should be typed as MDXModule
  const [exports, setExports] = useState<any>(undefined)

  useEffect(() => {
    evaluate(mdx, {
      Fragment: undefined,
      jsx: undefined,
      jsxs: undefined,
      ...runtime,
    }).then((exports) => setExports(exports))
  }, [mdx])

  return exports
}
import React from "react"
import { useMdx } from "./useMdx"

const components = {
  Foo: () => <div>Nice</div>,
}

type 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@wooorm
Comment options

Answer selected by peterj35-dd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants