Replace the MDX Content Wrapper with an MDXProvider in Gatsby MDX

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 4 years ago

MDX content has a root element called wrapper. This element is a React.Fragment by default which can lead to some warnings if you pass props to the MDX component generated from a file, like gatsby-mdx's src/pages support does. Here's how to fix that.

Instructor: [00:00] Here, we have some MDX, a running Gatsby project, and the result of that MDX displayed on the page. Note that we have an MDX provider wrapping our entire application with no components being passed in currently.

[00:12] When using an MDX file in source pages in a Gatsby project, you may see this invalid prop warning in the console. This is because the default wrapping element for MDX content is a fragment and Gatsby will pass in any page context to the page component.

[00:26] In this case, since we're using an MDX file as a page, that means the MDX file will get any page context passed in as a prop, and react.fragment cannot take any props. To rectify this, we can replace wrapper with a fragment, but not passing in any of the extra props.

[00:45] Note that when our application refreshes, we no longer see the fragment error because we are no longer passing in the props to the fragment.

egghead
egghead
~ 13 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today