react-i18next Namespace Not Loading — Fix Missing Keys
In this fix, you'll learn how to use namespaced translation files. This matters for organizing translations by feature, and appears in large apps with many translation files.
The Problem
The following code demonstrates the typical mistake:
const { t } = useTranslation();
return <h1>{t('title')}</h1>;
// title key is in 'dashboard' namespace, not default
// Renders 'title' (the key) instead of 'Dashboard'
The Fix
Here is the corrected version:
const { t } = useTranslation('dashboard');
return <h1>{t('title')}</h1>;
// Renders 'Dashboard' correctly
Prevention
- Always specify the namespace in useTranslation(ns) when using namespaced files.
- Pass multiple namespaces as an array: useTranslation(['common', 'dashboard']).
- Set defaultNS in i18n init to avoid specifying it everywhere.
- Use ns option in i18n.init() to load namespaces eagerly.
- Organize translations by feature/page for better maintainability.
Common Mistakes with i18n namespace
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
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