【问题标题】:Using React i18next do I have to use withTranslation?使用 React i18next 我必须使用 withTranslation 吗?
【发布时间】:2020-03-30 17:26:53
【问题描述】:
我想知道一些事情。
假设我有一个组件,我想访问 t 值。我是否必须将其连接到 redux。既然用react-i18next我也在用i18next我就不能这样:
import i18next from 'i18next'
class Comp extends Component {
render() {
return <Text>{i18next.t('space')}</Text>
}
}
或者这样做有什么缺点?我的翻译似乎仍然有效,但肯定有缺点吗?我猜只有当语言环境发生变化时它才会看到更新?还有什么?
谢谢。
【问题讨论】:
标签:
reactjs
i18next
react-i18next
【解决方案1】:
你不能在 React 组件类上使用钩子(至少没有 HOC)。
您可以做的最好的事情是导入 withTranslation 并通过此组件导出类。
我的意思:
// Other imports
import { withTranslation } from "react-i18next";
class ClassComponent extends React.Component {
//Code of your class
}
export default withTranslation()(ClassComponent);
请注意,您应该在渲染时调用 t 和 this.props.t。