【问题标题】:How working I18nextProvider, withNamespaces in in react-i18next?在 react-i18next 中如何使用 I18nextProvider,withNamespaces?
【发布时间】:2019-08-16 12:04:48
【问题描述】:

我在我的应用程序中使用 I18nextProviderwithNamespaces 来 react-i18next。

export const App = () => (
    <I18nextProvider i18n={i18n}>
      <App />
    </I18nextProvider>
);
export default withNamespaces()(ChildComponentOfApp);

但我不明白I18nextProviderwithNamespaces 的关系。他们没有传递像t函数这样的道具

provider 负责将 props 传入的 i18next 实例向下传递给所有 withNamespaces hocs 或 NamespacesConsumer 使用 react context api 的渲染 prop。

是为什么工作?这是withNamespaces 中的渲染道具吗?

我知道withNamespaces 是HOC,但即使在阅读react-i18next 源代码之后,也会混淆I18nextProviderwithNamespaces 之间的这种关系。

谁能解释一下?

【问题讨论】:

    标签: reactjs react-i18next


    【解决方案1】:

    I18nextProvider -> 你传入一个 prop "i18n",其中包含要使用的 i18next 实例 -> Provider 使用 react 的上下文 API 将其向下传递到 JSX 树

    withNamespaces -> 从上下文中获取 i18n 实例并将 t 函数传递给您的组件。

    【讨论】:

    • 非常感谢@jamuhl!我比以前明白了很多。好的,我的下一步是使用官方文档中 react 的 context API 检查 JSX 树
    猜你喜欢
    • 1970-01-01
    • 2019-04-08
    • 2022-12-28
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2021-09-01
    • 2019-07-13
    • 1970-01-01
    相关资源
    最近更新 更多