Skip to content

Commit

Permalink
Merge pull request #1286 from TQjona/master
Browse files Browse the repository at this point in the history
refactor: remove unneeded object
  • Loading branch information
jamuhl authored Mar 26, 2021
2 parents ffbe35e + 740c4e3 commit e83da8a
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/useTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ export function useTranslation(ns, props = {}) {

// binding t function to namespace (acts also as rerender trigger)
function getT() {
return {
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0]),
};
return i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0]);
}
const [t, setT] = useState(getT()); // seems we can't have functions as value -> wrap it in obj
const [t, setT] = useState(getT);

const isMounted = useRef(true);
useEffect(() => {
Expand All @@ -55,12 +53,12 @@ export function useTranslation(ns, props = {}) {
// in side effect and do not call resetT if unmounted
if (!ready && !useSuspense) {
loadNamespaces(i18n, namespaces, () => {
if (isMounted.current) setT(getT());
if (isMounted.current) setT(getT);
});
}

function boundReset() {
if (isMounted.current) setT(getT());
if (isMounted.current) setT(getT);
}

// bind events to trigger change, like languageChanged
Expand All @@ -81,13 +79,13 @@ export function useTranslation(ns, props = {}) {
const isInitial = useRef(true);
useEffect(() => {
if (isMounted.current && !isInitial.current) {
setT(getT());
setT(getT);
}
isInitial.current = false;
}, [i18n]); // re-run when i18n instance was replaced

const ret = [t.t, i18n, ready];
ret.t = t.t;
const ret = [t, i18n, ready];
ret.t = t;
ret.i18n = i18n;
ret.ready = ready;

Expand Down

0 comments on commit e83da8a

Please sign in to comment.