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

154
Visualizações
Conditional lazy loading import in React.js

I am lazy loading timeago language strings, when i had only one language i had one import + the buildFormatter :

import frenchStrings from 'react-timeago/lib/language-strings/fr';
import buildFormatter from 'react-timeago/lib/formatters/buildFormatter';
const formatter = buildFormatter(frenchStrings);

And this is how i display the date in the app

<TimeAgo date={data.date} formatter={formatter} />

But now that i have 3 languages, i had to use the lazy loading :

import TimeAgo from 'react-timeago';
import React from 'react';
let langString;
if (locale === 'en') {
  langString = React.lazy(()=>import('react-timeago/lib/language-strings/en'));
} else if (locale === 'nl') {
  langString = React.lazy(()=> import('react-timeago/lib/language-strings/nl'));
} else {
  langString = React.lazy(()=>import('react-timeago/lib/language-strings/fr'));
}

I am getting the used language from the HTML file and using it with props

const TheLocale = root_el.getAttribute("data-locale");

To put the current language strings in the formatter i have added a switch condition, i am not sure if i really need this condition!

let Strings;
switch(TheLocale) {
  case 'en': {
    Strings = englishStrings;
    break;
  }
  case 'nl': {
    Strings = dutchStrings;
    break;
  }
  default:
    Strings = frenchStrings;
}

const formatter = buildFormatter(Strings);

The formatter is not working, and im not sure if the import are correct

about 4 years ago · Juan Pablo Isaza
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