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

153
Views
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
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!