@@ -10,7 +10,7 @@ export function useTranslation(ns, props = {}) {
10
10
if ( i18n && ! i18n . reportNamespaces ) i18n . reportNamespaces = new ReportNamespaces ( ) ;
11
11
if ( ! i18n ) {
12
12
warnOnce ( 'You will need to pass in an i18next instance by using initReactI18next' ) ;
13
- const notReadyT = k => ( Array . isArray ( k ) ? k [ k . length - 1 ] : k ) ;
13
+ const notReadyT = ( k ) => ( Array . isArray ( k ) ? k [ k . length - 1 ] : k ) ;
14
14
const retNotReady = [ notReadyT , { } , false ] ;
15
15
retNotReady . t = notReadyT ;
16
16
retNotReady . i18n = { } ;
@@ -19,7 +19,9 @@ export function useTranslation(ns, props = {}) {
19
19
}
20
20
21
21
if ( i18n . options . react && i18n . options . react . wait !== undefined )
22
- warnOnce ( 'It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.' ) ;
22
+ warnOnce (
23
+ 'It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.' ,
24
+ ) ;
23
25
24
26
const i18nOptions = { ...getDefaults ( ) , ...i18n . options . react , ...props } ;
25
27
const { useSuspense } = i18nOptions ;
@@ -34,7 +36,7 @@ export function useTranslation(ns, props = {}) {
34
36
// are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
35
37
const ready =
36
38
( i18n . isInitialized || i18n . initializedStoreOnce ) &&
37
- namespaces . every ( n => hasLoadedNamespace ( n , i18n , i18nOptions ) ) ;
39
+ namespaces . every ( ( n ) => hasLoadedNamespace ( n , i18n , i18nOptions ) ) ;
38
40
39
41
// binding t function to namespace (acts also as rerender trigger)
40
42
function getT ( ) {
@@ -68,12 +70,22 @@ export function useTranslation(ns, props = {}) {
68
70
// unbinding on unmount
69
71
return ( ) => {
70
72
isMounted . current = false ;
71
- if ( bindI18n && i18n ) bindI18n . split ( ' ' ) . forEach ( e => i18n . off ( e , boundReset ) ) ;
73
+ if ( bindI18n && i18n ) bindI18n . split ( ' ' ) . forEach ( ( e ) => i18n . off ( e , boundReset ) ) ;
72
74
if ( bindI18nStore && i18n )
73
- bindI18nStore . split ( ' ' ) . forEach ( e => i18n . store . off ( e , boundReset ) ) ;
75
+ bindI18nStore . split ( ' ' ) . forEach ( ( e ) => i18n . store . off ( e , boundReset ) ) ;
74
76
} ;
75
77
} , [ namespaces . join ( ) ] ) ; // re-run effect whenever list of namespaces changes
76
78
79
+ // t is correctly initialized by useState hook. We only need to update it after i18n
80
+ // instance was replaced (for example in the provider).
81
+ const isInitial = useRef ( true ) ;
82
+ useEffect ( ( ) => {
83
+ if ( isMounted . current && ! isInitial . current ) {
84
+ setT ( getT ( ) ) ;
85
+ }
86
+ isInitial . current = false ;
87
+ } , [ i18n ] ) ; // re-run when i18n instance was replaced
88
+
77
89
const ret = [ t . t , i18n , ready ] ;
78
90
ret . t = t . t ;
79
91
ret . i18n = i18n ;
@@ -86,7 +98,7 @@ export function useTranslation(ns, props = {}) {
86
98
if ( ! ready && ! useSuspense ) return ret ;
87
99
88
100
// not yet loaded namespaces -> load them -> and trigger suspense
89
- throw new Promise ( resolve => {
101
+ throw new Promise ( ( resolve ) => {
90
102
loadNamespaces ( i18n , namespaces , ( ) => {
91
103
resolve ( ) ;
92
104
} ) ;
0 commit comments