Passing variables to use inside MDX file

See original GitHub issue

Hi, I’m using gatsby-plugin-mdx to add rich markdown to my site.

I want to access the frontmatter of the markdown within the markdown itself.

The docs say this is available in my MDX file with {props.pageContext.frontmatter} but this returns an error “TypeError: Cannot read property ‘frontmatter’ of undefined”.

I presume this is because, rather than rendering as an individual page, I’m trying to render this mardown inside a component using MDXRenderer. I’m guessing that in this case “props.pageContext…” doesn’t work.

(The actual use case is for a portfolio site - I’m trying to loop through all my “Work” markdown files on a single “Featured Work” page and render each one individually)

I expect that the way to do this is by passing the a variable using the MDXRenderer component. Something like:

<MDXRenderer category={work.frontmatter.category}>{work.body}</MDXRenderer>

But then I’m not sure how to access the category property inside my MDX markdown.

Question:

Is this the correct way to pass variables into my MDX code? Is what I want to do even possible? And if so, how do I then access the variables inside my MDX file?

Thanks in advance, B

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
mrbjjacksoncommented, Sep 11, 2019

Update:

After doing more searching I’ve found this thread on Spectrum that explained that any properties passed in <MDXRenderer> are available in the {props} object in the mdx file.

e.g. in my case I can do

<MDXRenderer frontmatter={work.frontmatter}>{work.body}</MDXRenderer>

and then in the mdx markup I can add a component and use the {props} object. E.g.

<Socials links={props.frontmatter.socials}/>

Funny, I could have sworn I tried that while I was debugging but apparently not well enough.

0reactions
cwkang1998commented, Jul 1, 2020

I still don’t see this information in the official docs, and it confused me until I find this thread. Mind I ask if I may add this as documentation for mdx in the Programmatically creating pages section as suggested by n-laverenko? I think it would help a lot of people.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using MDX
This article explains how to use MDX files in your project. It shows how you can pass props and how to import, define,...
Read more >
Solved: Passing parameters to MDX source queries
I have a list of employee names in my parameter dropdown (populated using a query). I want to pass the parameter value selected...
Read more >
How to use Gatsby env variables in a mdx file? - Stack Overflow
I can access to my variables inside my .js files, but not in my .mdx files. What I am missing? Is it the...
Read more >
Guide to Writing MDX and Markdown | Chicago Docs
Overview. The official MDX site describes MDX as 'an authorable format that lets you seamlessly write JSX in your Markdown documents.
Read more >
Passing Parameter to MDX QUERY like prepare variables in sql
Even in the xaction file, it declared the "condition" variable but i do ... I use {myvar} all the time in mdx queries,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found