如何在react-router中使用react-loadable?

穆罕默德

我正在尝试实现具有10-15页的应用程序。可以很好地与react-router一起工作,但我应该使用react-loadable来获得Spinner和加载效果...

但是,如何将路由器组件导入可加载项中?

我应该为每个组件创建一个const变量吗?

像这样 :

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

const News = Loadable({
    loader: () => import('./News'),
    loading: Loading,
});

const Gallery = Loadable({
    loader: () => import('./Gallery'),
    loading: Loading,
});

class App extends React.Component {
    render() {
        return (
          <Router>
              <Route exact path="/" component={Home} />
              <Route path="/news" component={News} />
              <Route path="/gallery" component={Gallery} />
          </Router>
        )
    }
}

还是其他技巧也有可能?

乌卡沙

这有点棘手。您不需要使用react-loadable。

演示在这里

如果要为图像和其他组件制作加载器onLoad,请使用react-content-loader来创建框架屏幕(请参见components/Image.js演示)。它可以使“几乎”完美的装载机。到目前为止,这是我能做的。我不知道要检测onLoadCSS :(

您可以创建一个routes.js包含所有页面的新文件

- /src
-- /pages
--- Gallery.js
--- Home.js
--- News.js
--- withBase.js
-- App.js
-- routes.js

routes.js

import Home from './pages/Home';
import Gallery from './pages/Gallery';
import News from './pages/News';
// Add as much as you want...

export default [
   {
       path: '/',
       component: Home,
   },
   {
       path: '/gallery',
       component: Gallery,
   },
   {
       path: '/news',
       component: News,
   },
];

您需要创建一个将包装每个页面的高级组件。

withBase.js(HOC)

import React from "react";

export default WrappedComponent =>
  class extends React.Component {
    state = {
      isLoading: true
    };

    componentDidMount() {
      this.hideLoader();
    }

    hideLoader = () => {
      // This is for demo purpose
      const proc = new Promise(resolve => {
        setTimeout(() => resolve(), 3000);
      });
      proc.then(() => this.setState({ isLoading: false }));
    };

    render() {
      return (
        <div>
          {this.state.isLoading ? <p>Loading...</p> : <WrappedComponent />}
        </div>
      );
    }
  };

用法:例如export default withBase(Home)

然后,在App.js回路中循环。

    <Switch>
      {routes.map(route => (
        <Route
          exact
          key={route.path}
          path={route.path}
          component={route.component}
        />
      ))}
    </Switch>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用React Loadable和React Router基于路由进行预取?

使用Loadable从react-loadable拆分代码会导致屏幕闪烁

我应该使用react-loadable或loadable-components进行代码拆分吗?

使用react-loadable加载页面的错误方式

react-loadable-对象作为React子对象无效

如何在Electron中使用React Router?

如何在React Router中使用HistoryLocation?

如何在React VR中使用React React Router?

React Loadable 在动态加载的组件中请求新的 css

React-loadable仅从相对路径获取块

从多类导出js的react-loadable导入

React-Loadable重新渲染导致输入失去焦点

加载完成时的react-loadable回调?

如何在React.net中使用react-router

如何在React中使用React Router检测路由变化?

如何在React Router中使用React.createContext()?

使用create-react-app和react-loadable进行代码拆分不起作用

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

如何在React Router Dom中使用嵌套路由

如何在React-Router的组件中使用参数?

如何在模型中使用React-Router

如何在Match.params中使用React Router?

如何在 {this.props.children} 中使用 React-router

如何在方法中使用 react-router

延迟加载:react-loadable 无法加载其他文件夹中的组件

React Loadable SSR 并不总是给客户端完整的包列表,导致页面闪烁

在使用 @loadable/component 渲染之前等待 promise

如何在React Router DOM中使用Regex从路由匹配中排除参数?

如何在Reactjs的新的react-router-dom中使用重定向