Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions react-components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ icon: "react"
---

import { Counter } from "/snippets/counter.mdx";
import { ColorGenerator } from "/snippets/color-generator.mdx";
import { ColorGenerator } from "/snippets/color-generator.jsx";

[React components](https://react.dev) are a powerful way to create interactive and reusable elements in your documentation.

Expand Down Expand Up @@ -49,7 +49,7 @@ And the component will be rendered as a React component in the MDX file.
Just like in regular React, you can import components from other files.

```mdx
import { ColorGenerator } from "/snippets/color-generator.mdx"
import { ColorGenerator } from "/snippets/color-generator.jsx"
```

<Warning>
Expand All @@ -68,7 +68,7 @@ Learn more about [reusable snippets](/reusable-snippets).

You can also build much more complex components. Here's an example of a color generator component that uses multiple React hooks:

```mdx /snippets/color-generator.mdx [expandable]
```mdx /snippets/color-generator.jsx [expandable]
export const ColorGenerator = () => {
const [hue, setHue] = useState(180)
const [saturation, setSaturation] = useState(50)
Expand Down
29 changes: 29 additions & 0 deletions reusable-snippets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,32 @@ import { myName, myObject } from '/snippets/path/to/custom-variables.mdx';

Hello, my name is {myName} and I like {myObject.fruit}.
```

### JSX Snippets

1. Export a JSX component from your `.jsx` snippet file ([support React components](/react-components)):

```js icon=square-js snippets/my-jsx-snippet.jsx
export const MyJSXSnippet = () => {
return (
<div>
<h1>Hello, world!</h1>
</div>
)
}
```

<Warning>
Important: When creating JSX snippets, use arrow function syntax (=>) rather than function declarations. The `function` keyword is not supported in this context.
</Warning>

2. Import the snippet from your destination file and use the component:

```typescript destination-file.mdx
---
title: My title
description: My Description
---

import { MyJSXSnippet } from '/snippets/my-jsx-snippet.jsx';
```
File renamed without changes.