Creation:2025-02-07Last update:2025-06-29
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本编辑此文档
如果您有改善此文档的想法,请随时通过在GitHub上提交拉取请求来贡献。
文档的 GitHub 链接Copy
复制文档 Markdown 到剪贴板
React 集成:useDictionary 钩子文档
本节提供了在 React 应用中使用 useDictionary 钩子的详细指导,使得无需视觉编辑器即可高效处理本地化内容。
React 中的示例用法
./ComponentExample.tsx
复制代码
复制代码到剪贴板
import type { FC } from "react";
import { useDictionary } from "react-intlayer";
import componentContent from "./component.content";
const ComponentExample: FC = () => {
const { title, content } = useDictionary(componentContent);
return (
<div>
<h1>{title}</h1>
<p>{content}</p>
</div>
);
};
服务器集成
如果您在 IntlayerProvider 之外使用 useDictionary 钩子,则在渲染组件时必须显式提供 locale 作为参数:
./ServerComponentExample.tsx
复制代码
复制代码到剪贴板
import type { FC } from "react";
import { useDictionary } from "react-intlayer/server";
import clientComponentExampleContent from "./component.content";
const ServerComponentExample: FC<{ locale: string }> = ({ locale }) => {
const { content } = useDictionary(clientComponentExampleContent, locale);
return (
<div>
<h1>{content.title}</h1>
<p>{content.content}</p>
</div>
);
};
