1

我想知道一些事情。

假设我有一个组件并且我想访问该t值。我是否必须将其连接到 redux。由于使用react-i18next我也在使用我i18next不能只是这样做:

import i18next from 'i18next'

class Comp extends Component {
    render() {
      return <Text>{i18next.t('space')}</Text>
    }
}

或者这样做有什么缺点?我的翻译似乎仍然有效,但肯定有缺点吗?我猜只有当语言环境发生变化时它才会看到更新?还要别的吗?

谢谢。

4

1 回答 1

0

你不能在 React 组件类上使用钩子(至少没有 HOC)。

您可以做的最好的事情是通过此组件导入withTranslation和导出类。

我的意思是说:

// Other imports
import { withTranslation } from "react-i18next";

class ClassComponent extends React.Component {
    //Code of your class
}

export default withTranslation()(ClassComponent);

请注意,您应该在渲染时调用twith 。this.props.t

于 2020-03-30T09:26:53.050 回答