Skip to content

Commit 1584a44

Browse files
authored
add regression test for optional chaining issue (#1856)
* add regression test for optional chaining issue * avoid exception when passing bindI18n: false
1 parent d515c68 commit 1584a44

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/useTranslation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export const useTranslation = (ns, props = {}) => {
132132
// unbinding on unmount
133133
return () => {
134134
isMounted.current = false;
135-
if (i18n) bindI18n?.split(' ').forEach((e) => i18n.off(e, boundReset));
135+
if (i18n && bindI18n) bindI18n?.split(' ').forEach((e) => i18n.off(e, boundReset));
136136
if (bindI18nStore && i18n)
137137
bindI18nStore.split(' ').forEach((e) => i18n.store.off(e, boundReset));
138138
};

src/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export const hasLoadedNamespace = (ns, i18n, options = {}) => {
7272
lng: options.lng,
7373
precheck: (i18nInstance, loadNotPending) => {
7474
if (
75-
options.bindI18n?.indexOf('languageChanging') > -1 &&
75+
options.bindI18n &&
76+
options.bindI18n.indexOf('languageChanging') > -1 &&
7677
i18nInstance.services.backendConnector.backend &&
7778
i18nInstance.isLanguageChangingTo &&
7879
!loadNotPending(i18nInstance.isLanguageChangingTo, ns)

test/useTranslation.spec.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ describe('useTranslation', () => {
186186
<I18nextProvider i18n={i18n}>{children}</I18nextProvider>
187187
);
188188

189+
it('should not fail when passing bindI18n: false or undefined', () => {
190+
expect(() =>
191+
renderHook(() => useTranslation('translation', { bindI18n: false })),
192+
).to.not.throw();
193+
expect(() =>
194+
renderHook(() => useTranslation('translation', { bindI18n: undefined })),
195+
).to.not.throw();
196+
});
197+
189198
it('should render correct content', () => {
190199
const { result: resultNoLng } = renderHook(() => useTranslation('translation'), {
191200
wrapper,

0 commit comments

Comments
 (0)