Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!