【发布时间】:2020-04-11 04:06:30
【问题描述】:
我正在使用带有 react-i18next 的 React
我的 index.tsx 文件包含一些组件,我可以在那里使用翻译功能
index.js
import React, { Suspense } from 'react'
import ReactDOM from 'react-dom';
import * as utils from './Utils';
import './i18n';
import { useTranslation, withTranslation, Trans } from 'react-i18next';
...
const { t, i18n } = useTranslation();
//I can use the translate function here
t("title");
//call a util function
utils.helperFunction(...);
...
这里一切正常。 我现在在一个附加文件中创建了一些辅助函数
Utils.tsx
...
import { useTranslation, withTranslation, Trans } from 'react-i18next';
...
export function helperFunction(props: any){
//do stuff
//now I need some translation here - but useTranslation is not working?
const { t, i18n } = useTranslation();
t("needTranslation");
}
如何在辅助函数中使用相同的翻译逻辑? (并不总是将t 函数传递给辅助方法)
或者这里使用钩子是错误的方法?
出现以下错误
React Hook "useTranslation" is called in function "helperFunction" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
【问题讨论】:
标签: javascript reactjs react-i18next