import { ChangeEvent, useState } from "react"; import { RuleType } from "async-validator"; import { Avatar, Box, Container, FormHelperText, Link, Paper, Typography, Hidden, Button } from "@mui/material"; import { makeStyles } from "@mui/styles"; import { ValidatedForm, ValidatedTextField } from "@/components/Validated"; import useAsyncFunc from "@/utils/useAsyncFunc"; import { FetchUser, login, sendVerify } from "@/api"; import { LoadingButton } from "@mui/lab"; import { useForm } from "../Validated/context"; import { useSnackbar } from "notistack"; import { useConfirm } from "material-ui-confirm"; import { useTranslation } from "react-i18next"; const { t } = useTranslation(); const rules = { username1: { type: "string" as RuleType, required: true, message: "User Name is required" }, password1: { type: "string" as RuleType, required: true, message: "Password is required" }, code: { type: "string" as RuleType, required: true, message: "Verify code is required" } }; const withoutRules = { username1: { type: "string" as RuleType, required: true, message: "User Name is required" }, password1: { type: "string" as RuleType, required: true, message: "Password is required" } }; function Copyright() { const { t } = useTranslation(); return ( <Typography variant="body2" color="textSecondary" align="center"> {t("Copyright") + " © " + new Date().getFullYear()}{" "} <Link color="inherit" href="."> ZBeats, Inc. </Link>{" "} {t("All rights reserved.")} </Typography> ); } 那么 const { t } = useTranslation(); 应如何声明
时间: 2023-07-05 15:32:05 浏览: 204
`const { t } = useTranslation();` 应该被声明在函数组件的顶层,以便在组件内的任何地方都可以使用 `t` 函数获取翻译字符串。例如:
```
import React from "react";
import { useTranslation } from "react-i18next";
const MyComponent = () => {
const { t } = useTranslation();
return (
<div>
<h1>{t("Welcome")}</h1>
<p>{t("This is a localized string.")}</p>
</div>
);
};
export default MyComponent;
```
在这个例子中,`useTranslation()` hook 被调用并返回一个对象,其中包含 `t` 函数和其他一些有用的属性和方法。`{ t }` 这一行将 `t` 函数从该对象中解构出来,以便在组件中使用。
阅读全文