Skip to content

Commit a26522e

Browse files
fix: back key useEffect
1 parent 2a01d1c commit a26522e

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

packages/use-i18n/src/__tests__/usei18n.test.tsx

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('i18n hook', () => {
117117
const spy = vi.spyOn(console, 'error')
118118
spy.mockImplementation(() => {})
119119

120-
expect(() => renderHook(() => useTranslation())).toThrow(
120+
expect(() => renderHook(() => useTranslation(['test']))).toThrow(
121121
new Error('useTranslation must be used within a I18nProvider'),
122122
)
123123
spy.mockRestore()
@@ -133,34 +133,41 @@ describe('i18n hook', () => {
133133
spy.mockRestore()
134134
})
135135

136-
it('should use defaultLoad, useTranslation, switch local and translate', async () => {
137-
const { result } = renderHook(() => useTranslation<Locale, Locales>([]), {
138-
wrapper: wrapper({ defaultLocale: 'en' }),
139-
})
140-
// first render there is no load
141-
expect(result.current.t('title')).toEqual('')
136+
it(
137+
'should use defaultLoad, useTranslation, switch local and translate',
138+
async () => {
139+
const { result } = renderHook(
140+
() => useTranslation<Locale, Locales>(['test']),
141+
{
142+
wrapper: wrapper({ defaultLocale: 'en' }),
143+
},
144+
)
145+
// first render there is no load
146+
expect(result.current.t('title')).toEqual('')
142147

143-
await waitFor(() => {
144-
// after load of en locale
145-
expect(result.current.t('title')).toEqual(en.title)
146-
})
148+
await waitFor(() => {
149+
// after load of en locale
150+
expect(result.current.t('title')).toEqual(en.title)
151+
})
147152

148-
await act(async () => {
149-
await result.current.switchLocale('fr')
150-
})
153+
await act(async () => {
154+
await result.current.switchLocale('fr')
155+
})
151156

152-
await waitFor(() => {
153-
expect(result.current.t('title')).toEqual(fr.title)
154-
})
157+
await waitFor(() => {
158+
expect(result.current.t('title')).toEqual(fr.title)
159+
})
155160

156-
await act(async () => {
157-
await result.current.switchLocale('es')
158-
})
161+
await act(async () => {
162+
await result.current.switchLocale('es')
163+
})
159164

160-
await waitFor(() => {
161-
expect(result.current.t('title')).toEqual(es.title)
162-
})
163-
})
165+
await waitFor(() => {
166+
expect(result.current.t('title')).toEqual(es.title)
167+
})
168+
},
169+
{},
170+
)
164171

165172
it('should use specific load on useTranslation', async () => {
166173
const { result } = renderHook(
@@ -262,7 +269,8 @@ describe('i18n hook', () => {
262269

263270
it('should work with a component', async () => {
264271
const { result } = renderHook(
265-
() => useTranslation<{ 'with.identifier': 'Hello {identifier}' }>([]),
272+
() =>
273+
useTranslation<{ 'with.identifier': 'Hello {identifier}' }>(['test']),
266274
{
267275
wrapper: wrapper({ defaultLocale: 'en' }),
268276
},

packages/use-i18n/src/usei18n.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,15 @@ export function useTranslation<
162162
}
163163
const { loadTranslations, namespaces: loadedNamespaces } = context
164164

165+
// here we generate a key string from the array of namespace in order to only trigger the use effect
166+
// when the passed keys change and not when the ref of the array change
167+
const key = namespaces.join(',')
165168
useEffect(() => {
166169
// eslint-disable-next-line @typescript-eslint/no-floating-promises
167-
namespaces.map(async (namespace: string) =>
168-
loadTranslations(namespace, load),
169-
)
170-
}, [loadTranslations, namespaces, load])
170+
key
171+
.split(',')
172+
.map(async (namespace: string) => loadTranslations(namespace, load))
173+
}, [loadTranslations, key, load])
171174

172175
const isLoaded = useMemo(
173176
() => areNamespacesLoaded(namespaces, loadedNamespaces),

0 commit comments

Comments
 (0)