Creating a custom snippet
Pre-condition: You must create your snippet file in thesnippets
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.
Default export
- 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
- Import the snippet into your destination file.
destination-file.mdx
Exporting with variables
- 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
- Import the snippet into your destination file with the variable. The property will fill in based on your specification.
destination-file.mdx
Reusable variables
- Export a variable from your snippet file:
snippets/path/to/custom-variables.mdx
- Import the snippet from your destination file and use the variable:
destination-file.mdx
JSX snippets
- Export a JSX component from your snippet file. (See React components for more information):
snippets/my-jsx-snippet.jsx
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