小编jhi*_*jhi的帖子

React 延迟组件未加载到动态路由上

我在动态路由上使用了 react lazy 和 suspense,但不知何故我无法渲染延迟加载的组件。

我已经搜索了关于在路由上使用 lazy 的信息,但我还没有看到有人在动态(localhost:8080/ dynamic / dynamic)路由上使用它。

在动态路由上加载组件对我有用,如果我有静态路由,延迟加载也适用,但是当我尝试将两者结合时,组件不会加载。

这是我所做的一个例子,

import Loader from './path/toFile';
import Home from './path/toFile';
const LazyLoadedComponent = lazy(() => import('./path/toFile'));

const App = () => {
 return(
  <Router>
     <Switch>

        // Home has multiple views controlled by buttons
        // A view contains selections with corresponding id
       <Route exact path="/:viewType?" component={Home} /> 

       // this component doesn't load when I select something which links me to this dynamic route.
       <Suspense fallback={Loader}>
         <Route path="/viewType/:selectionId" component={LazyLoadedComponent} /> …
Run Code Online (Sandbox Code Playgroud)

lazy-loading reactjs react-router react-suspense

6
推荐指数
2
解决办法
5261
查看次数