3

I recently read about React Lazy and how it "loads" the components during run time when they are required to be rendered. I assume that "loading" here means fetching the component from the server and then rendering it.

So my question is, how does React manage this fetching of components? How does it know the exact path from where to fetch this component (given that our code will mention the relative path but fetching will require complete server path)? Does it depend on Webpack for this?

2
  • read this article 👉 blog.logrocket.com/… Commented Jul 4, 2019 at 11:07
  • @malbarmawi The post does not answer my question.. Commented Jul 4, 2019 at 11:34

1 Answer 1

4

Let's look into the React code. React.lazy is defined as follows.

export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
  let lazyType = {
    $$typeof: REACT_LAZY_TYPE,
    _ctor: ctor,
    // React uses these fields to store the result.
    _status: -1,
    _result: null,
  };

  if (__DEV__) {
    // ... additional code only in development mode
  }

  return lazyType;
}

As you can see, React.lazy requires a Promise which resolves to a module with a default export containing a React component (freely cited by React Docs). This also means that not React resolves the file, but import() does. import() works as documented in the MDN.

The async import() is a new function in ES6 which is not available in all browsers but can be polyfilled by Webpack and Babel/Typescript/others.

What you often see is code like the following, which automatically splits the imported file away by Webpack.


import(/* webpackChunkName: "xyz" */ './component/XYZ')

This creates a new javascript xyz.js next to your bundle script.

If you don't use Webpack, you need to create those files by yourself. Webpack just reduces the work required from you. So you don't absolutely depend on Webpack. This approach might look like the following:

// ./component/xyz.js
export default function() { return <div>Component</div> }

// ./main.js
const OtherComponent = React.lazy(() => import('./component/xyz.js'));
export default function() { return <div>Component</div> }

And the file structure:

| public
|---| main.js
|---| component 
|---| --- | main.js

As you see, no webpack required. It just makes your life easier.

Sign up to request clarification or add additional context in comments.

9 Comments

I find it a bit surprising that React is implicitly depending on Webpack (or any other module bundler) for one of it's core features. It doesn't even mention this caveat in the docs. Even if React is relying on import, import won't work if Webpack had not placed the component file in the right place..
React does not depend on Webpack. You can build your file structure by yourself, but I guess it's harder to maintain. Webpack on the other hand resolves the files by itself and puts the files into the right place accordingly.
So I believe Webpack replaces the path in import statement during build time?
Yes, see Webpack Docs. But this requires the comment /* webpackChunkName: "xyz" */ inside the import statement. Without that, it's not a separate chunk name (except if configured otherwise in webpack.config.js)
So we need to manually configure webpack.config.js for every dynamic import?
|

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.