-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lazy load component with path stored in a variable or prop #16132
Comments
After reading this comment, I looked into Webpack and saw that this is a limitation of dynamic expressions in webpack https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import. I know in advance which files should be bundled but would still need the ability to dynamically import them. Is there any way to do this in React? Related links |
This isn't related to React, not much we can do here. Would recommend reaching out to webpack instead. |
@ReasonableDeveloper You may consider using loadable-components instead of React.lazy. take a look at this section in their docs. (link) |
A simple solution I come up with is like this: export const LINKS = [
{
component: './tabs/someNameComponent',
name: 'someName',
icon: 'mdi mdi-truck',
},
{
component: './tabs/anotherNameComponent',
name: 'anotherName',
icon: 'mdi mdi-card',
},
];
export const SUB_PAGES = LINKS.map(({ name, component }) => {
return {
path: name,
component: lazy(() => import(`${component}`)), // THIS IS THE TRICK
};
}); |
It is crazy and interesting that only |
Note again this has nothing to do with React. Whether (and how) this works depends entirely on your bundler. |
I have the same issue any suggestions? |
It's not working for me (NextJS) and I have a suspicion that maybe it's working for you just because it imports ALL modules in the project - just try to break some non-relevant module and you will see a build error. |
"The import() must contain at least some information about where the module is located. Bundling..." I just read here too |
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
Error and warning when trying to dynamically
lazy
load a component from a variable or props.Reproduction
In a Codesandbox it seems to be working https://codesandbox.io/s/angry-rgb-vs0g4 fine. For some reason in a non codesandboxed environment, I was able to reproduce the issue here.
What is the expected behavior?
It should lazily load the component even when the path is passed down via props or stored in a variable without throwing a warning or error. The behavior of following code:
should match the behavior of
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Latest version of CRA (3.0.1) and latest React (16.8.6)
This issue was first reported in the lerna support CRA pull request seen here.
The text was updated successfully, but these errors were encountered: