react-i18next Translation Not Updating — Fix Language Switch
In this fix, you'll learn why translations don't update after changeLanguage. This matters for multilingual app support, and appears in language switchers in internationalized apps.
The Problem
The following code demonstrates the typical mistake:
import i18n from './i18n';
function Greeting() {
return <h1>{i18n.t('greeting')}</h1>;
}
// Doesn't update when language changes — no reactive binding
The Fix
Here is the corrected version:
import { useTranslation } from 'react-i18next';
function Greeting() {
const { t, i18n } = useTranslation();
return <h1>{t('greeting')}</h1>;
}
// Updates reactively when language changes
Prevention
- Always use useTranslation hook in components that display translated text.
- Call i18n.changeLanguage() to switch languages — it triggers re-renders.
- Initialize i18n with react: { useSuspense: true } for Suspense integration.
- Use Trans component for translations with embedded HTML or components.
- Use t('key', { defaultValue: 'Fallback' }) for development without translation files.
Common Mistakes with i18n translation
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro