Skip to content

Commit

Permalink
Fix hooks being called conditionally in useTranslation
Browse files Browse the repository at this point in the history
In the `useTranslation` hook, `useContext` is called conditionally, which breaks the Rule of Hooks, so it may cause problems in the future. This fixes it by calling `useContext` on every render, just not always using the result from it.
  • Loading branch information
onestopjs authored Sep 7, 2019
1 parent f41a24b commit 3d2361f
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/useTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { warnOnce, loadNamespaces, hasLoadedNamespace } from './utils';
export function useTranslation(ns, props = {}) {
// assert we have the needed i18nInstance
const { i18n: i18nFromProps } = props;
const ReactI18nContext = useContext(I18nContext);
const { i18n: i18nFromContext, defaultNS: defaultNSFromContext } = getHasUsedI18nextProvider()
? useContext(I18nContext) || {}
? ReactI18nContext || {}
: {};
const i18n = i18nFromProps || i18nFromContext || getI18n();
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
Expand Down

0 comments on commit 3d2361f

Please sign in to comment.