Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-i18next suspense

I m using React-i18next just like the example

import React, { Suspense } from 'react';
import { useTranslation } from 'react-i18next';

function App() {
  return (
    <Suspense fallback="loading">
      <MyComponent />
   </Suspense>
   );
}

But Suspense is breaking one of my other components, namely react-masonry-layout. Is it possible to not using Suspense?

Thanks.

like image 438
Cheung Brian Avatar asked Nov 15 '19 05:11

Cheung Brian


People also ask

What is withTranslation in react?

The withTranslation is a classic HOC (higher order component) and gets the t function and i18n instance inside your component via props. import React from 'react'; import { withTranslation } from 'react-i18next'; ​ function MyComponent({ t, i18n }) {

How is useTranslation used in functions?

You can fix it by moving the useTranslation hook to the MyNav component body and pass the translation function t as a closure in getMyMenu . import { useTranslation } from "react-i18next"; export const MyNav = props => { const { t } = useTranslation('translations'); function getMyMenu(a) { if (a.

What is i18next?

i18next is an internationalization-framework written in and for JavaScript. But it's much more than that! i18next goes beyond just providing the standard i18n features such as (plurals, context, interpolation, format). It provides you with a complete solution to localize your product from web to mobile and desktop.


1 Answers

react-i18nnext uses Suspense by default. If you don't want to use it, you have to specify that in your configuration. If you have a i18n config file, you can set the useSuspense flag to false in the react section of the init object.

//Example config
i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
   fallbackLng: "en",
   debug: true,
   resources: {

   },
   interpolation: {
     escapeValue: false,
   },
  react: {
     wait: true,
     useSuspense: false,
  },
})

Or you can just set the flag in your component.

<MyComponent useSuspense={false} />

Just be aware that choosing not to use Suspense has its implications. You have to write checks to handle the 'not ready' state ie: have a loading component render when state is not ready and your component render when the state is ready.Not doing so will result in rendering your translations before they loaded.

like image 169
rivnatmalsa Avatar answered Oct 08 '22 14:10

rivnatmalsa