简体   繁体   English

使用 react-loadable 延迟加载组件的动态路径导入

[英]Dynamic path import for lazy loading of components using react-loadable

I am creating an app using create-react-app and lazy loading the component using react-loadable.我正在使用 create-react-app 创建一个应用程序,并使用 react-loadable 延迟加载组件。

What I want to do is import dynamic paths for the loader object or the Loadable function of react-loadble module.我想要做的是为 loader 对象或 react-loadble 模块的 Loadable 函数导入动态路径。

Code:代码:

const LoadableComponent = path =>
 Loadable({
  loader: () => import(`${path}`),
  loading: Loader,
 })

const Home = LoadableComponent('./../../pages/Home')
const User = LoadableComponent('./../../pages/User')

If I put the path string in the place of the path variable (Ex. import('./pages/Home')) and call the function it works.如果我将路径字符串放在路径变量的位置(例如 import('./pages/Home'))并调用它工作的函数。 But when I use it like the code above Loader will load but it will not continue to load the component anymore.但是当我像上面的代码一样使用它时,Loader 将加载但它不会再继续加载组件。 Why doesn't it work if I use the variable in import?如果我在导入中使用变量,为什么它不起作用?

Found the answer here这里找到答案

"For Webpack to handle an import, it needs to be able to at least guess roughly what an import() is meant to be referencing." “对于 Webpack 处理导入,它至少需要能够粗略地猜测 import() 意味着引用什么。”

Turns out your path cannot be too anonymous.原来你的路径不能太匿名。 Guess it is stacked to deep for Webpack to confidently know what I am importing.猜猜它堆得很深,Webpack 可以自信地知道我在导入什么。

As far as I know, the bundler (Webpack) needs to be able to process the file path upfront.据我所知,捆绑器(Webpack)需要能够预先处理文件路径。

My question would be why is it required?我的问题是为什么需要它? Is there anything so bad about just writing?仅仅写作有什么不好的地方吗?

const Home = Loadable({
  loader: () => import('./pages/Home'),
  loading: Loader,
})

const User = Loadable({
  loader: () => import('./pages/User'),
  loading: Loader,
})

Bonus point: Take a look at new React's lazy API , recently release in 16.6.加分点:看看最近在 16.6 中发布的新 React 的lazy API The linked page also has some good information on code splitting.链接页面也有一些关于代码拆分的好信息。

To simplify config lazy loading,the solution likes below:为了简化配置延迟加载,解决方案如下:

import React, {Component, lazy, Suspense} from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter} from "react-router-dom";
let routers={
    '/about': import('./pages/about'),
    '/contact':  import('./pages/contact'),
}

class Content() extends Component{
   render(){
     return (<Switch>
        {Object.keys(this.props.routers).map((path, idx) => {
            let Child = lazy(()=>this.props.routers[path])
            return (
                <Route path={path} key={idx}>
                    <Suspense fallback={<div>Loading...</div>}>
                        <Child></Child>
                    </Suspense>
                </Route>)
        })}
    </Switch>
   }
}

ReactDOM.render(
    (
    <BrowserRouter>
        <div>
            <ul>
                <li><Link to="/about"> About</Link></li>
                <li><Link to="/contact">Contact</Link></li>
            </ul>
            <Content routers={config.routers}></Content>
        </div>
    </BrowserRouter>
    ),
    document.getElementById('root') as HTMLElement
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM