-
Notifications
You must be signed in to change notification settings - Fork 390
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
Memoized string-only translation does not update on useLingui
re-render
#1690
Comments
It's just a mistake in the docs. Indeed, you should add |
Wait, did you try to depend on |
Thanks for the clarification. Not updating due to the stable identity makes sense. Unfortunately, this means that many projects will get an const { i18n } = useLingui();
const { t } = i18n;
const welcome = useMemo(() => {
return t`welcomeMessage`;
}, [t]); Or is thing something that we should rather do as a custom hook in our project? |
You will not able to do this as custom hook, because Actually using global const { i18n } = useLingui();
const welcome = useMemo(() => {
return t(i18n)`welcomeMessage`;
}, [i18n]); |
This still doesn't work except when adding import { t } from '@lingui/macro';
import { useLingui } from '@lingui/react';
const Component = () => {
const { i18n } = useLingui();
const welcome = useMemo(() => {
return t(i18n)`welcomeMessage`;
}, [i18n, i18n.locale]);
return welcome;
}; Am I missing something? |
@vonovak You added this section in docs https://lingui.dev/tutorials/react-patterns#memoization-pitfall could you elaborate a bit here? Does |
hello, my mistake, sorry. I wrote the docs badly. You can depend on |
What is the context object? |
sorry I didn't make it clear. This will be more robust because it will react to updates to message catalogs as well. |
@vonovak For me depending on the |
hello, I updated the docs here https://github.com/lingui/js-lingui/pull/1701/files |
@vonovak Sorry I was not clear: I used the |
Both versions below do indeed work, though I wish there was a shorter way to do a memoized string-only translation: import { t } from '@lingui/macro';
import { useLingui } from '@lingui/react';
const Component = () => {
const lingui = useLingui();
// Works
const version1 = useMemo(() => {
return t(lingui.i18n)`welcomeMessage`;
}, [lingui]);
// Works
const version2 = useMemo(() => {
return lingui.i18n.t("welcomeMessage");
}, [lingui]);
[...]
}; |
I'm away for a few days now but believe there is a way to make this less verbose: we can expose the 't' function from the context in such a way that it will change reference as we need. |
@niksauer @tingyuzeng18 @thekip I opened the PR here #1721; feedback is welcome |
Describe the bug
In v4, to ensure that messages tagged with the
t
macro get updated when the locale changes, we must subscribe components to theuseLingui
hook. This works reliably in the component's return-to-render. However, if parts of the render are memoized using a hook such asuseMemo
, the value will not always be updated. Always, because this seems to only happen after switching the locale at least twice. The first change is registered. Yet, wheni18n.locale
is added to the hook's dependencies, values are reliably re-computed.To Reproduce
Does not work reliably:
Works reliably:
Additional context
Add any other context about the problem here.
4.2.0
@babel/core@7.21.5
(via@lingui/cli@4.2.0
)5.0.1
The text was updated successfully, but these errors were encountered: