Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove unneeded object #1286

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was the reason for this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the original author of the code just tried useState(getT()) with getT() returning the i18n.t function. React detected that the parameter passed to useState was a function and thought it was a lazy-init function (useState(() => initialValue)). So it was calling i18n.t with no args and used the return value as initial value, which was probably null or undefined.

Basically to store a function in a state you have to have a function which returns the function, e.g. useState(() => (args) => console.log(args)) and setState(() => (args) => console.log(args))

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