-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
React.lazy usage with useTranslation hook #817
Comments
Could you provide a code sandbox for reproduction? Not yet sure - never used React.lazy yet - so not sure what it does under the hood...but as it's react official i guess it should work with hooks out of the box... |
Sure! Let me get one together for you. 😄 |
thank you |
Arg. I can't reproduce it in a CodeSandbox because the issue happens at a very specific point in time. I've setup a router with a lazy loaded route. I've also setup a context provider for my authenticated user details. When a user is authenticated and refreshes the page, all components that are lazily-loaded do not have translations. It seems to be a very edge case.Perhaps when full Suspense drops, this will not longer be an issue. Until then, I'll stop using React.lazy 😄 Sorry to bother! |
Hm, strange...never had tested react.lazy...just assumed everything will work. Hopefully this get sorted out sooner or later - will also try to find some time to test and see what i can do - currently hard on time before weekend. |
Actually, looks like it's still happening even when not using React.lazy. @jamuhl would you take a reproduction repo? |
Okay, I figured out the problem! It seems I was doing this:
When I updated all to use It seems the resolve to figure out what namespace was taking too long and therefore loading the translations wasn't working? |
hm...that is a sync one-liner: https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L29 |
I’ll put together a repo reproduction. It seems to not happen in codesandbox, but does in CRA. |
I have also encountered issues with using this with React.lazy, for components that are wrapped The page I'm trying to load is the
|
@YuhanLee please provide a codesandbox for reproduction |
@jamuhl the issue with "please provide a codesandbox for reproduction" is that the issue doesn't seem to be reproducible in CodeSandbox. Now that someone else has validated the issue, can I share my actual project code with you in lieu of a CodeSandbox? |
@DevanB don't have to be codesandbox...but minimal...remove everything not needed....I do not have the time to look at any actual large project |
Sounds good. I'll try to get that together by the weekend.
The issue with react-loadable (to me) is that it isn't the official React solution; |
I'll get back to you as soon as I can produce the issue. |
@jamuhl @DevanB I'm having the same issue where some components are seemly randomly unable to call t('...') to get back data (( t('test') will return 'test' as if the key doesn't exist )), while other components are able to. I did try switching my router from React Lazy to react-loadable, but the issue is still present. I'll try to post something with more context once I can better isolate the problem in my large app. I don't think the problem is to do with React.lazy since replacing it with react-loadable had no effect for me... there's something else going on I think. I did notice that sometimes changing to a different page in the router and then switching back to problematic page causes it to work. Perhaps it's something to do with loading order.. |
Never mind, it was a user error. I was using a sub page "/test/foo" and my i18n localization file was set to a relative directory (so it was looking for /test/en.json instead of /en.json). Setting the location to being with "/" fixed the issue of the translation file not loading in. |
I'm experiencing problems with the hooks API as well. In our application, namespaces are dynamically added depending on user roles. Our problem is the other way round though: It only works for lazily loaded components. Normal components are not re-rendered with an updated Could it be that there's a problem with re-rendering when new namespaces come in and that the inconsistent results related to I've tried coming up with a minimal reproduction but also failed to get the exact same behavior outside of our application. However, it behaves a bit weird in a different way: Namespaces that are dynamically added to In my quest to narrow down the problem, I've noticed a few things. Just leaving them here, maybe it rings a bell for someone:
I'd appreciate any ideas that might help debugging this! |
@nicolasschabram not sure if that is related...changing the namespace in The loading happens only once in a useEffect because of https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L77 -> you might change the empty array to // ...
if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
if (bindI18nStore && i18n)
bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
};
}, [ns]); // define props to trigger using effect on rerender (none here) Could you please check if that solves your problem? |
Hi! I have a problem using In the I'm not sure if this is related to the Could someone help me? |
Sorry, but I'm not used to webcomponents...I also do not get an error using chrome -> the Tab shows in blue like expected...no error in console.... |
Hi @jamuhl. |
@CristianDDias try React.useEffect(() => {
if (
!ref.current ||
!ref.current.shadowRoot ||
!ref.current.shadowRoot.firstElementChild
)
return;
const content = ref.current.shadowRoot.firstElementChild.getElementsByClassName(
"ui5-tc__content"
)[0];
content.style.background = "blue";
content.style.padding = "8px";
}); |
@CristianDDias did that work for you? |
@jamuhl, actually it didn't work :/ because even using the It needs to change the |
@CristianDDias Guess for now in this case you will have to work with useSuspense false for those components you need to access the shadowRoot. |
Closing this for now...feel free to reopen if still an issue |
During some of my browsing, I got to this issue. I am using In our app, we are using multiple namespaces, and the lazy-loaded component is using a different namespace. This behaviour is "caused" because of this. Basically, because the component is lazy-loaded, React starts to render the other components when it thinks they are ready. Since this component is loaded at a later stage, the At a later stage, i18next detects that it needs to load some additional namespaces (Based on usage within other components) and loads them to the store. However, the components themselves aren't re-rendered since nothing really changed in them (All props, state etc. are the same) so a "missingKey" is displayed. This describes the process (In concept)
I actually think this behaviour (Of lazy-loaded) components with react-i18next should be documented somehow. |
because of this...idk...not really...that is only a take default if nothing else is needed...like said...I never could reproduce this....and we have some lazy-loaded stuff in our production app. A case for proper reproduction would help... |
@jamuhl So here's a quick reproduction that I've created https://codesandbox.io/s/react-i18next-lazy-loading-example-zve2k I don't think that's a bug or something.. Understanding why this behavior happens is enough in my opinion. Maybe something about this should be written somewhere? (Or not, you know best whether people had this situation) |
@slavab89 but your sample is totally wrong...you're using namespaces inside a component but do not define that inside useTranslation -> how should it load that at all...using something like: |
@jamuhl I tried to modify @slavab89's codesandbox example as follow - const { t } = useTranslation();
+ const { t } = useTranslation("lazy"); But the example still fails to load the lazy-loaded translation: Also, @slavab89 had commented that line, which seems to show that he tried it, so maybe I'm just not understanding your answer. I'm myself failing to get useTranslation working with lazy components wrapped in suspense. Would you mind sharing an example since you say doing that in your production app? My code looks like this: const Admin: React.FC = () => {
const { t } = useTranslation("admin"); // <-- Calling namespace in useTranslation, this should trigger loadNamespace !
return (
<div>
{t("admin:dashboard")}
</div>
);
}; const Loadable = (importStatement) => {
const Component = lazy(importStatement); // <-- The component is lazy !
return (props: Record<string, unknown>) => (
<Suspense fallback={<div>...</div>}> // <-- The lazy component is wrapped in Suspense !!
<Component {...props} />
</Suspense>
);
}; import Loadable from "./components/Loadable";
const AdminPage = Loadable(() => import("./pages/Admin"));
export default function App(): JSX.Element {
return (
<div>
<ErrorBoundary fallback={<h2>failed</h2>}>
<Suspense fallback={<h2>loading</h2>}>
<Route path="/admin/">
<AdminPage />
</Route>
</Suspense>
</ErrorBoundary>
</div>
);
} This throws the following error Note that if disabling suspense, I don't have any problem. But that's a shame when having react concurrent mode enabled... const Admin: React.FC = () => {
const { t, ready } = useTranslation("admin", { useSuspense: false });
return ready ? (
<div>
{t("admin:dashboard")}
</div>
) : null;
}; |
Idk...just made the change again: https://codesandbox.io/s/react-i18next-lazy-loading-example-forked-kv2cr works on my browser... |
Describe the bug
I have a few pages that use
React.lazy
. It seems that on those pages, my translations aren't loaded (or maybe are loaded after the component renders).I've tried several variations of setting
react: { useSuspense: false}
anduseTranslation('translations'
, {useSuspense: false})` but have had no success getting translations to display.I'm not sure if this is a bug, or a misconfiguration on my part.
Occurs in react-i18next version
10.6.1
To Reproduce
i18n.ts
Optionally add
react: { useSuspense: false}
useTranslation
hook:Optionally use
useTranslation('translations'
, {useSuspense: false})`Expected behaviour
I expect to see my translations, not the key of the translation.
OS (please complete the following information):
The text was updated successfully, but these errors were encountered: