question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React.lazy usage with useTranslation hook

See original GitHub issue

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} and useTranslation('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

  1. Use the following config: i18n.ts
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-xhr-backend';
import {initReactI18next} from 'react-i18next';

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    defaultNS: 'translation',
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
    load: 'languageOnly',
    saveMissing: true,
  });

export default i18n;

Optionally add react: { useSuspense: false}

  1. Lazy load a component that uses the useTranslation hook:
const NotFound = React.lazy(() => import('./NotFound'));
import React from 'react';
import {useTranslation} from 'react-i18next';

const NotFound: React.SFC = () => {
  const [t] = useTranslation();
  return <h1 data-testid='not-found-heading'>{t('not-found')}</h1>;
};

export default NotFound;

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):

  • Device: 2016 MBP 13" macOS 10.14.4
  • Browser: Firefox Developer Edition 67.0b6

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:37 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
jamuhlcommented, Sep 8, 2020

Idk…just made the change again: https://codesandbox.io/s/react-i18next-lazy-loading-example-forked-kv2cr works on my browser…

1reaction
jamuhlcommented, Jun 27, 2020

@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: t("lazy:lazyContent") does not magically load the namespace or assert it is loaded…if you’re using namespaces define them in useTranslation…that’s it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add lazy loading to React i18next with React Suspense
Step 3: Add React Suspense to your React application. By default react-i18next withTranslation HOC or the useTranslation hook will expect you to use...
Read more >
useTranslation (hook) - react-i18next documentation
When to use? Use the useTranslation hook inside your functional components to access the translation function or i18n instance.
Read more >
reactjs - react-i18next - import i18n instance & use directly vs ...
I'm trying to use react-i18next to support translations. ... know why is it recommended to use the useTranslation hook / withTranslation HOC ...
Read more >
A Guide to React Localization with i18next | Phrase
React -i18next is a powerful set of components, hooks, and plugins that sit on top of i18next. Learn how to use it to...
Read more >
Concurrent UI Patterns (Experimental) - React
We will need to use this Hook's return values to set up our state transition. There are two values returned from useTransition :...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found