1

This is very bizarre. During my attempt at code-splitting, I encountered this:

const g = "bi";
const importStr = `react-icons/${g.toLowerCase()}`;
console.log(importStr);
console.log(importStr === `react-icons/bi`);

import(importStr).then(module => {
    console.log(module);
});



import(`react-icons/bi`).then(module => {
    console.log(module);

});

In the above code, if I import "importStr", then the import throws an error:

Uncaught (in promise) Error: Cannot find module 'react-icons/bi'

But if I directly import "react-icons/bi", then there is no issue. As you see,

importStr === `react-icons/bi`

Why and how do I fix this? I can't actually directly use "react-icons/bi" because g is dynamic and could take other values.

9
  • This gets run through a bundler? It might not be able to compute importStr properly. Commented Aug 20, 2021 at 22:38
  • That may be the reason. But how can I dynamically import 'react-icons/bi' or 'react-icons/fa' or any other subgroups of icons depending on the needs? Commented Aug 20, 2021 at 22:42
  • That will depend on your bundler, what are you using? Webpack? Rollup? Parcel? Commented Aug 20, 2021 at 22:44
  • I wonder what would happen if you wrap your import by function like function work(str) { import(str).then(console.log) } and then call it work(`react-icons/${g.toLowerCase()}`); Commented Aug 20, 2021 at 22:50
  • webpack. The app was created using "create react app" Commented Aug 20, 2021 at 22:50

2 Answers 2

2

I quote from the following comment

Webpack performs a static analyse at build time. It doesn't try to infer variables which that import(test) could be anything, hence the failure. It is also the case for import(path+"a.js").

Because of the tree shaking feature webpack tries to remove all unused code from the bundle. That also means something similar could work

import("react-icons/" + g)

Edit: as per your suggestion I updating this to

import("react-icons/" + g.toLowerCase()+ '/index.js')
Sign up to request clarification or add additional context in comments.

Comments

0

An easy way to implement code splitting in React is with lazy loading, as seen here (this might be easier than importing a dynamic string):

const OtherComponent = React.lazy(() => import('./OtherComponent'));

This implementation will load the bundle with OtherComponent only when it is first rendered.

Example from reactjs.org:

import React, { Suspense } from 'react';

const OtherComponent = React.lazy(() => import('./OtherComponent'));
const AnotherComponent = React.lazy(() => import('./AnotherComponent'));

function MyComponent() {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <section>
          <OtherComponent />
          <AnotherComponent />
        </section>
      </Suspense>
    </div>
  );
}

More info on React.lazy

1 Comment

I think his issue is dynamic import paths where he is not able to build path from variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.