组件
React
使用 JSX、状态和客户端逻辑,在你的 Mintlify 文档中通过自定义 React 组件构建交互且可复用的元素。
直接在 MDX 文件中使用 React 组件
直接在你的 MDX 文件中声明组件:
export const Counter = () => {
const [count , setCount ] = useState (0 )
const increment = () => setCount (count + 1 )
const decrement = () => setCount (count - 1 )
return (
< div >
< button onClick = { decrement } > -</ button >
< span > { count } </ span >
< button onClick = { increment } > +</ button >
</ div >
)
}
< Counter />
export const Counter = () => {
const [count, setCount] = useState(0)
const increment = () => setCount(count + 1)
const decrement = () => setCount(count - 1)
return (
<div>
<button onClick={decrement}>-</button>
<span>{count}</span>
<button onClick={increment}>+</button>
</div>
)
}
<Counter />Mintlify 中的 React 组件运行在沙盒化的 MDX 环境中,并受到以下限制:
- React hooks 已预先注入:
useState、useEffect、useRef、useCallback、useMemo、useContext和useReducer无需导入即可使用。 - npm 包需要额外设置:要导入第三方包,请设置第三方库。否则,请使用浏览器内置功能或直接编写内联逻辑。
- 不支持默认导出:请使用具名导出(
export const MyComponent = ...)。不支持默认导出(export default)。 - 每个文件声明自己的导入:如果某个片段使用了另一个片段,请在该片段文件中导入它。页面上的导入不会作用于页面所导入的片段。参见嵌套片段。
- 不支持 JSON 导入:不支持导入
.json文件。 - 不支持代码拆分:不支持
React.lazy和动态import()。页面上的所有组件代码(包括导入的片段)都会编译进页面并随页面一起加载。
组件文件是 .jsx 或 .tsx 文件,你可以将它们放在项目中的任意位置。更多信息请参阅 可复用片段。
在 snippets/ 中创建一个组件文件:
/snippets/color-generator.jsx
export const ColorGenerator = () => {
const [hue , setHue ] = useState (180 )
const [saturation , setSaturation ] = useState (50 )
const [lightness , setLightness ] = useState (50 )
const [colors , setColors ] = useState ([])
useEffect (() => {
const newColors = []
for (let i = 0 ; i < 5 ; i ++ ) {
const l = Math .max (10 , Math .min (90 , lightness - 20 + i * 10 ))
newColors .push (`hsl( ${ hue } , ${ saturation } %, ${ l } %)` )
}
setColors (newColors )
}, [hue , saturation , lightness ])
const copyToClipboard = (color ) => {
navigator .clipboard
.writeText (color )
.then (() => {
console .log (`Copied ${ color } to clipboard!` )
})
.catch ((err ) => {
console .error ("Failed to copy: " , err )
})
}
return (
< div className = "p-4 border dark:border-zinc-950/80 rounded-xl not-prose" >
< div className = "space-y-4" >
< div className = "space-y-2" >
< label className = "block text-sm text-zinc-950/70 dark:text-white/70" >
Hue: { hue } °
< input
type = "range"
min = "0"
max = "360"
value = { hue }
onChange = { (e ) => setHue (Number .parseInt (e .target .value ))}
className = "w-full h-2 bg-zinc-950/20 rounded-lg appearance-none cursor-pointer dark:bg-white/20 mt-1"
style = { {
background: `linear-gradient(to right,
hsl(0, ${ saturation } %, ${ lightness } %),
hsl(60, ${ saturation } %, ${ lightness } %),
hsl(120, ${ saturation } %, ${ lightness } %),
hsl(180, ${ saturation } %, ${ lightness } %),
hsl(240, ${ saturation } %, ${ lightness } %),
hsl(300, ${ saturation } %, ${ lightness } %),
hsl(360, ${ saturation } %, ${ lightness } %))` ,
}}
/>
</ label >
< label className = "block text-sm text-zinc-950/70 dark:text-white/70" >
Saturation: { saturation } %
< input
type = "range"
min = "0"
max = "100"
value = { saturation }
onChange = { (e ) => setSaturation (Number .parseInt (e .target .value ))}
className = "w-full h-2 bg-zinc-950/20 rounded-lg appearance-none cursor-pointer dark:bg-white/20 mt-1"
style = { {
background: `linear-gradient(to right,
hsl( ${ hue } , 0%, ${ lightness } %),
hsl( ${ hue } , 50%, ${ lightness } %),
hsl( ${ hue } , 100%, ${ lightness } %))` ,
}}
/>
</ label >
< label className = "block text-sm text-zinc-950/70 dark:text-white/70" >
Lightness: { lightness } %
< input
type = "range"
min = "0"
max = "100"
value = { lightness }
onChange = { (e ) => setLightness (Number .parseInt (e .target .value ))}
className = "w-full h-2 bg-zinc-950/20 rounded-lg appearance-none cursor-pointer dark:bg-white/20 mt-1"
style = { {
background: `linear-gradient(to right,
hsl( ${ hue } , ${ saturation } %, 0%),
hsl( ${ hue } , ${ saturation } %, 50%),
hsl( ${ hue } , ${ saturation } %, 100%))` ,
}}
/>
</ label >
</ div >
< div className = "flex space-x-1" >
{ colors .map ((color , idx ) => (
< div
key = { idx }
className = "h-16 rounded flex-1 cursor-pointer transition-transform hover:scale-105"
style = { { backgroundColor: color }}
title = { `Click to copy: ${ color } ` }
onClick = { () => copyToClipboard (color )}
/>
))}
</ div >
< div className = "text-sm font-mono text-zinc-950/70 dark:text-white/70" >
< p >
Base color: hsl({ hue } , { saturation } %, { lightness } %)
</ p >
</ div >
</ div >
</ div >
)
}
export const ColorGenerator = () => {
const [hue, setHue] = useState(180)
const [saturation, setSaturation] = useState(50)
const [lightness, setLightness] = useState(50)
const [colors, setColors] = useState([])
useEffect(() => {
const newColors = []
for (let i = 0; i < 5; i++) {
const l = Math.max(10, Math.min(90, lightness - 20 + i * 10))
newColors.push(`hsl(${hue}, ${saturation}%, ${l}%)`)
}
setColors(newColors)
}, [hue, saturation, lightness])
const copyToClipboard = (color) => {
navigator.clipboard
.writeText(color)
.then(() => {
console.log(`Copied ${color} to clipboard!`)
})
.catch((err) => {
console.error("Failed to copy: ", err)
})
}
return (
<div className="p-4 border dark:border-zinc-950/80 rounded-xl not-prose">
<div className="space-y-4">
<div className="space-y-2">
<label className="block text-sm text-zinc-950/70 dark:text-white/70">
Hue: {hue}°
<input
type="range"
min="0"
max="360"
value={hue}
onChange={(e) => setHue(Number.parseInt(e.target.value))}
className="w-full h-2 bg-zinc-950/20 rounded-lg appearance-none cursor-pointer dark:bg-white/20 mt-1"
style={{
background: `linear-gradient(to right,
hsl(0, ${saturation}%, ${lightness}%),
hsl(60, ${saturation}%, ${lightness}%),
hsl(120, ${saturation}%, ${lightness}%),
hsl(180, ${saturation}%, ${lightness}%),
hsl(240, ${saturation}%, ${lightness}%),
hsl(300, ${saturation}%, ${lightness}%),
hsl(360, ${saturation}%, ${lightness}%))`,
}}
/>
</label>
<label className="block text-sm text-zinc-950/70 dark:text-white/70">
Saturation: {saturation}%
<input
type="range"
min="0"
max="100"
value={saturation}
onChange={(e) => setSaturation(Number.parseInt(e.target.value))}
className="w-full h-2 bg-zinc-950/20 rounded-lg appearance-none cursor-pointer dark:bg-white/20 mt-1"
style={{
background: `linear-gradient(to right,
hsl(${hue}, 0%, ${lightness}%),
hsl(${hue}, 50%, ${lightness}%),
hsl(${hue}, 100%, ${lightness}%))`,
}}
/>
</label>
<label className="block text-sm text-zinc-950/70 dark:text-white/70">
Lightness: {lightness}%
<input
type="range"
min="0"
max="100"
value={lightness}
onChange={(e) => setLightness(Number.parseInt(e.target.value))}
className="w-full h-2 bg-zinc-950/20 rounded-lg appearance-none cursor-pointer dark:bg-white/20 mt-1"
style={{
background: `linear-gradient(to right,
hsl(${hue}, ${saturation}%, 0%),
hsl(${hue}, ${saturation}%, 50%),
hsl(${hue}, ${saturation}%, 100%))`,
}}
/>
</label>
</div>
<div className="flex space-x-1">
{colors.map((color, idx) => (
<div
key={idx}
className="h-16 rounded flex-1 cursor-pointer transition-transform hover:scale-105"
style={{ backgroundColor: color }}
title={`Click to copy: ${color}`}
onClick={() => copyToClipboard(color)}
/>
))}
</div>
<div className="text-sm font-mono text-zinc-950/70 dark:text-white/70">
<p>
Base color: hsl({hue}, {saturation}%, {lightness}%)
</p>
</div>
</div>
</div>
)
}然后导入并使用它:
import { ColorGenerator } from "/snippets/color-generator.jsx"
< ColorGenerator />
import { ColorGenerator } from "/snippets/color-generator.jsx"
<ColorGenerator />- 客户端渲染:大多数自定义组件在页面加载后于浏览器中渲染,因此不会出现在服务端渲染的 HTML 中。这适用于你从 snippet 中按名称导入的组件(例如
import { ColorGenerator } from "/snippets/color-generator.jsx"),以及你在 MDX 文件中使用export定义的大多数组件。你作为内容导入的 MDX snippet(例如import MySnippet from "/snippets/my-snippet.mdx")会在服务端渲染。使用export定义的组件如果是简单模板,也会在服务端渲染。它只能将 props 放入 HTML 元素和内置组件中,不能使用 state、hooks、函数调用、条件判断、事件处理程序或style属性。每个使用它的地方都只能传入纯字符串属性,例如<HeroCard title="Quickstart" href="/quickstart" />。 - SEO:由于自定义组件不在初始 HTML 中,搜索引擎可能无法完全索引其内容。
- 首次加载:在自定义组件渲染期间,访客可能会看到闪烁或布局偏移。对于必须出现在初始 HTML 中或加载时不能发生偏移的内容,请改用内置组件,例如卡片和分栏。大多数内置组件都会在服务端渲染。
- 可访问性:确保屏幕阅读器能够播报动态内容的变化。
- 优化依赖数组:仅在
useEffect中包含必要的依赖。 - 缓存昂贵操作:在适当之处使用
useMemo或useCallback。 - 减少重新渲染:将大型组件拆分为较小的组件。
- 懒加载:组件代码无法懒加载。所有组件都会随页面一起加载。如果某个组件在挂载时会执行资源密集型操作(例如获取数据或进行大量计算),请使用条件渲染,将其挂载推迟到用户交互时。