From f3272c583823484ff9db4f2ea8d5595392297c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Xalambr=C3=AD?= Date: Mon, 5 Aug 2024 14:54:23 -0500 Subject: [PATCH] Avoid calling changeLanguage if it's the same as locale (#201) 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`. --- src/react.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/react.tsx b/src/react.tsx index a947dad..47566fc 100644 --- a/src/react.tsx +++ b/src/react.tsx @@ -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]); }