Replace the MDX Content Wrapper with an MDXProvider in Gatsby MDX

Chris Biscardi
InstructorChris Biscardi

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 3 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.