Skip to content

Commit

Permalink
Avoid calling changeLanguage if it's the same as locale (#201)
Browse files Browse the repository at this point in the history
Right now if locale change the `changeLanguage` function is called, this
function triggers a re-render of every component using `useTranslation`.

Instead we can check if `i18n.language` is different to the locale and
only then call `changeLanguage`.
  • Loading branch information
sergiodxa authored Aug 5, 2024
1 parent a720631 commit f3272c5
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export function useLocale(localeKey = "locale"): string {
export function useChangeLanguage(locale: string) {
let { i18n } = useTranslation();
React.useEffect(() => {
i18n.changeLanguage(locale);
if (i18n.language !== locale) i18n.changeLanguage(locale);
}, [locale, i18n]);
}

0 comments on commit f3272c5

Please sign in to comment.