Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda