title | description | icon |
---|---|---|
Reusable Snippets |
Reusable, custom snippets to keep content in sync |
recycle |
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.
Hello world! This is my content I want to reuse across pages.
- Import the snippet into your destination file.
---
title: My title
description: My Description
---
import MySnippet from '/snippets/path/to/my-snippet.mdx';
## Header
Lorem 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.
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.
---
title: My title
description: My Description
---
import MySnippet from '/snippets/path/to/my-snippet.mdx';
## Header
Lorem impsum dolor sit amet.
<MySnippet word="bananas" />
- Export a variable from your snippet file:
export const myName = 'my name';
export const myObject = { fruit: 'strawberries' };
- Import the snippet from your destination file and use the variable:
---
title: My title
description: My Description
---
import { myName, myObject } from '/snippets/path/to/custom-variables.mdx';
Hello, my name is {myName} and I like {myObject.fruit}.
- Inside your snippet file, create a component that takes in props by exporting your component in the form of an arrow function.
export const MyComponent = ({ title }) => (
<div>
<h1>{title}</h1>
<p>... snippet content ...</p>
</div>
);
- Import the snippet into your destination file and pass in the props
---
title: My title
description: My Description
---
import { MyComponent } from '/snippets/custom-component.mdx';
Lorem ipsum dolor sit amet.
<MyComponent title={'Custom title'} />
By default, Mintlify employs server-side rendering, generating content
at build time. For client-side content loading, ensure to verify the
document
object's availability before initiating the rendering process.
{/* `setTimeout` simulates a React.useEffect, which is called after the component is mounted. */}
export const ClientComponent = () => {
if (typeof document === "undefined") {
return null;
} else {
setTimeout(() => {
const clientComponent = document.getElementById("client-component");
if (clientComponent) {
clientComponent.innerHTML = "Hello, client-side component!";
}
}, 1);
return <div id="client-component"></div>
}
}