react-i18next
react-i18next copied to clipboard
No React update when updating resource bundle in i18next
🐛 Bug Report
No triggered update in React with t function from useTranslation hook when adding a resource with i18next.addResourceBundle
To Reproduce
Codesandbox example: https://codesandbox.io/p/sandbox/i18next-add-resource-bundle-update-fail-example-r65fym
Rough steps to reproduce:
- Start a react app with some translations loaded.
- In some event, like a button click in the codesandbox example, overwrite a key of the currently used translation
// Index.js
import { StrictMode, Suspense } from "react";
import { createRoot } from "react-dom/client";
import i18next from "i18next";
import { initReactI18next } from "react-i18next";
i18next.use(initReactI18next).init({
lng: "en",
ns: "translation",
react: {
useSuspense: true,
},
resources: {
en: {
translation: {
app: {
greeting: "hi",
},
},
},
},
});
import App from "./App";
const rootElement = document.getElementById("root");
const root = createRoot(rootElement);
root.render(
<StrictMode>
<Suspense>
<App />
</Suspense>
</StrictMode>
);
// App.js
import { useTranslation } from "react-i18next";
import i18next from "i18next";
export default function App() {
const { t } = useTranslation("translation");
return (
<div className="App">
<h1>{t("app.greeting")}</h1>
<button
onClick={() => {
i18next.addResourceBundle(
"en",
"translation",
{
app: {
greeting: "hi again",
},
},
true,
true
);
console.log(i18next.store.data);
}}
>
Change Translation
</button>
</div>
);
}
Expected behavior
React should trigger a rerender and text should update.
Your Environment
- runtime version: firefox and chrome
- i18next version: 23.10.1
- os: Mac
- any other relevant information: react-i18next:14.1.0
an update can be triggered by calling
i18next.emit("languageChanged")
by default react-i18next only binds the languageChanged event: https://react.i18next.com/latest/i18next-instance
so in your case, just add this to your options:
bindI18nStore: "added"