useTranslation (hook)
What it does
It gets the t function and i18n instance inside your functional component.
import React from 'react';
import { useTranslation } from 'react-i18next';
export function MyComponent() {
const { t, i18n } = useTranslation(); // not passing any namespace will use the defaultNS (by default set to 'translation')
// or const [t, i18n] = useTranslation();
return <p>{t('my translated text')}</p>
}import React from 'react';
import { useTranslation } from 'react-i18next';
export function MyComponent() {
const { t, i18n } = useTranslation(); // not passing any namespace will use the defaultNS (by default set to 'translation')
// or const [t, i18n] = useTranslation();
return <p>{t($ => $['my translated text'])}</p>
}While most of the time you only need the t function to translate your content, you can also get the i18n instance (in order to change the language).
i18n.changeLanguage('en-US');When to use?
Use the useTranslation hook inside your functional components to access the translation function or i18n instance.
In this tutorial you'll find some ways on how to use this useTranslation hook.
You'll also see how to use it when you need to work with multiple namespaces.

useTranslation params
Loading namespaces
Only the t function is bound to the namespace, behind the scenes it uses the getFixedT function of i18next.
The i18n instance is the normal i18next instance. Not bound to anything special.
Overriding the i18next instance
Optional keyPrefix option
available in react-i18next version >= 11.12.0
depends on i18next version >= 20.6.0
Do not use the keyPrefix option if you want to use keys with prefixed namespace notation:
i.e.
Do not use the keyPrefix option if you want to use keys with prefixed namespace notation:
i.e.
Optional lng option
available in react-i18next version >= 12.3.1
Not using Suspense
Last updated