i.brod
i.brod

Reputation: 4603

How does React.suspense "handle" i18next backend loading?

I'm working with some pretty standard create-react-app boilerplate, which uses lazy loading and react-i18next as a translation library. This library uses i18next-http-backend to fetch the translation files from a remote API.

What i'm trying to understand, is how exactly React.suspense is able to "recognize" this asynchronous call, and show the fallback UI until it's done.

Index.ts file:

import "./i18n";//Notice this

  const container = document.getElementById("root");
  ReactDOM.render(
    <StylesProvider jss={jss}>
      <ThemeProvider theme={theme}>
        <React.StrictMode>
          <BrowserRouter>
            <Router />
          </BrowserRouter>
        </React.StrictMode>
      </ThemeProvider>
    </StylesProvider>,
    container
  );

i18n file:

i18n
  .use(Backend)
  .init({
    backend:{
      loadPath: 'https://someRemoteApi/dictionary',          
   })

Router:

const Home = lazy(() => import("../../modules/home/Home"));
const Router: React.FC = (props) => {
  return (
    <>
      <ErrorBoundary>
        <Suspense fallback={<div className={styles.loader}><Loader /></div>}>
            <Switch>
              <ProtectedRoute exact component={Home} path="/"/>   
               ...more routes           
        </Suspense>
      </ErrorBoundary>
    </>
  );
};

With this setup, to my amazement, the fallback is rendered on the screen, until this backend plugin finishes its job. I'm trying to understand the mechanics of it, and whether this can be leveraged for other async operations.

the React docs clearly state:

React.Suspense lets you specify the loading indicator in case some components in the tree below it are not yet ready to render. Today, lazy loading components is the only use case supported by <React.Suspense>:

Any clarification will be greatly appreciated.

Upvotes: 4

Views: 2054

Answers (1)

Soufiane Boutahlil
Soufiane Boutahlil

Reputation: 2604

The idea of Suspense is when a component throws a Promise (or anything that is called during the component’s render), React looks for the closest Suspense in order to display the fallback UI.

In your case, your components are using the useTranslate hook. When namespaces are not yet loaded, it throws a Promise and loads the namespaces. During the render phase, React catches the thrown Promise and looks for the closest Suspense component up the tree in order to display fallback UI.

This is a snippet from the hook useTranslation:

 // not yet loaded namespaces -> load them -> and trigger suspense
  throw new Promise((resolve) => {
    loadNamespaces(i18n, namespaces, () => {
      resolve();
    });
  });

You can check how the hook useTranslation works from here

Upvotes: 4

Related Questions