One of the core principles of software development is DRY (Don’t Repeat
Yourself), which applies to documentation as
well. If you find yourself repeating the same content in multiple places, you
should create a custom snippet to keep your content in sync.
Pre-condition: You must create your snippet file in the snippets directory in order for the import to work.
Any page in the snippets directory will be treated as a snippet and will not
be rendered into a standalone page. If you want to create a standalone page
from the snippet, import the snippet into another file and call it as a
component.
Add content to your snippet file that you want to re-use. Optionally, you can add variables that can be filled in via props
when you import the snippet. In this example, our variable is word.
snippets/my-snippet.mdx
Copy
Ask AI
Hello world! This is my content I want to reuse across pages.
Import the snippet into your destination file.
destination-file.mdx
Copy
Ask AI
---title: My titledescription: My Description---import MySnippet from '/snippets/path/to/my-snippet.mdx';## HeaderLorem impsum dolor sit amet.<MySnippet/>
Optionally, you can add variables that can be filled in via props when you import the snippet. In this example, our variable is word.
snippets/my-snippet.mdx
Copy
Ask AI
My keyword of the day is {word}.
Import the snippet into your destination file with the variable. The property will fill in based on your specification.
destination-file.mdx
Copy
Ask AI
---title: My titledescription: My Description---import MySnippet from '/snippets/path/to/my-snippet.mdx';## HeaderLorem impsum dolor sit amet.<MySnippet word="bananas" />
Import the snippet from your destination file and use the variable:
destination-file.mdx
Copy
Ask AI
---title: My titledescription: My Description---import { myName, myObject } from '/snippets/path/to/custom-variables.mdx';Hello, my name is {myName} and I like {myObject.fruit}.
Important: When creating JSX snippets, use arrow function syntax (=>) rather than function declarations. The function keyword is not supported in this context.
Import the snippet from your destination file and use the component:
destination-file.mdx
Copy
Ask AI
---title: My titledescription: My Description---import { MyJSXSnippet } from '/snippets/my-jsx-snippet.jsx';
Assistant
Responses are generated using AI and may contain mistakes.