Skip to content

Commit ddf253c

Browse files
authored
Update react page (#924)
1 parent 5bb60b1 commit ddf253c

File tree

1 file changed

+70
-36
lines changed

1 file changed

+70
-36
lines changed

react-components.mdx

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,101 @@ description: "Build interactive and reusable elements with React components"
44
icon: "react"
55
---
66

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

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

12-
You can use React components directly in your `MDX` files without any additional setup.
13-
1411
## Using React components
1512

16-
You can build components directly in your MDX files using [React hooks](https://react.dev/reference/react/hooks).
13+
You can build React components directly in your `MDX` files using [React hooks](https://react.dev/reference/react/hooks).
1714

18-
### Basic example
15+
### Example
1916

20-
Here is a basic example of a counter component:
17+
This example declares a `Counter` component and then uses it with `<Counter />`.
2118

2219
```mdx
2320
export const Counter = () => {
24-
const [count, setCount] = useState(0);
25-
21+
const [count, setCount] = useState(0)
22+
23+
const increment = () => setCount(count + 1)
24+
const decrement = () => setCount(count - 1)
25+
2626
return (
27-
<div>
28-
<p>Current count: {count}</p>
29-
<button onClick={() => setCount(count + 1)}>
30-
+
31-
</button>
27+
<div className="flex items-center justify-center">
28+
<div className="flex items-center rounded-xl overflow-hidden border border-zinc-950/20 dark:border-white/20">
29+
<button
30+
onClick={decrement}
31+
className="flex items-center justify-center h-8 w-8 text-zinc-950/80 dark:text-white/80 border-r border-zinc-950/20 dark:border-white/20"
32+
aria-label="Decrease"
33+
>
34+
-
35+
</button>
36+
37+
<div className="flex text-sm items-center justify-center h-8 px-6 text-zinc-950/80 dark:text-white/80 font-medium min-w-[4rem] text-center">
38+
{count}
39+
</div>
40+
41+
<button
42+
onClick={increment}
43+
className="flex items-center justify-center h-8 w-8 text-zinc-950/80 dark:text-white/80 border-l border-zinc-950/20 dark:border-white/20"
44+
aria-label="Increase"
45+
>
46+
+
47+
</button>
48+
</div>
3249
</div>
33-
);
50+
)
3451
}
35-
```
3652

37-
The `Counter` component can be used in your `MDX` files like this:
38-
39-
```mdx
4053
<Counter />
4154
```
4255

43-
The counter renders as an interactive React component.
56+
export const Counter = () => {
57+
const [count, setCount] = useState(0)
4458

45-
<Counter />
59+
const increment = () => setCount(count + 1)
60+
const decrement = () => setCount(count - 1)
4661

47-
## Importing components
62+
return (
63+
<div className="flex items-center justify-center">
64+
<div className="flex items-center rounded-xl overflow-hidden border border-zinc-950/20 dark:border-white/20">
65+
<button
66+
onClick={decrement}
67+
className="flex items-center justify-center h-8 w-8 text-zinc-950/80 dark:text-white/80 border-r border-zinc-950/20 dark:border-white/20"
68+
aria-label="Decrease"
69+
>
70+
-
71+
</button>
72+
73+
<div className="flex text-sm items-center justify-center h-8 px-6 text-zinc-950/80 dark:text-white/80 font-medium min-w-[4rem] text-center">
74+
{count}
75+
</div>
4876

49-
Just like in regular React, you can import components from other files.
77+
<button
78+
onClick={increment}
79+
className="flex items-center justify-center h-8 w-8 text-zinc-950/80 dark:text-white/80 border-l border-zinc-950/20 dark:border-white/20"
80+
aria-label="Increase"
81+
>
82+
+
83+
</button>
84+
</div>
85+
</div>
86+
)
87+
}
5088

51-
```mdx
52-
import { ColorGenerator } from "/snippets/color-generator.jsx"
53-
```
89+
The counter renders as an interactive React component.
5490

55-
<Warning>
56-
Unlike regular React, you can't import components from every `MDX` file. Reusable components can only be referenced from `MDX` files within the `snippets` folder.
57-
</Warning>
91+
<Counter />
5892

59-
After importing the component, use it in your `MDX` files like this:
93+
## Importing components
6094

61-
```mdx
62-
<ColorGenerator />
63-
```
95+
You can import components from your `snippets` folder. Unlike regular React, you cannot import components from every `MDX` file. Reusable components must be referenced from files within the `snippets` folder. Learn more about [reusable snippets](/reusable-snippets).
6496

65-
Learn more about [reusable snippets](/reusable-snippets).
97+
### Example
6698

67-
### Complex example
99+
This example declares a `ColorGenerator` component that uses multiple React hooks and then uses it in an `MDX` file.
68100

69-
You can build much more complex components. Here is an example of a color generator component that uses multiple React hooks:
101+
Create `color-generator.jsx` file in the `snippets` folder:
70102

71103
```mdx /snippets/color-generator.jsx [expandable]
72104
export const ColorGenerator = () => {
@@ -181,9 +213,11 @@ export const ColorGenerator = () => {
181213
}
182214
```
183215

184-
The `ColorGenerator` component can be used in your `MDX` files like this:
216+
Import the `ColorGenerator` component and use it in an `MDX` file:
185217

186218
```mdx
219+
import { ColorGenerator } from "/snippets/color-generator.jsx"
220+
187221
<ColorGenerator />
188222
```
189223

0 commit comments

Comments
 (0)