Does react-router-v4 supports dynamic routing?

血红的双手。 提交于 2019-12-11 05:28:54

问题


I tried to add routes dynamically by using v3 but it didn't support it. React-router-v4 supports dynamic routing or not?


回答1:


It's quite unclear to me what you actually mean by dynamic routing? Are you thinking of routes provided by the API, or any sort other external source?

Sure you can, but you got to think your structure through. The easiest way to achieve that is to map them inside of your Switch Component:

<Switch>
  { routesList.map((route) => (
      <Route
        key={route._id}
        path={route.path}
        component={route.component}
        {...route.props} // some custom props, maybe?
      />
    )
  )}

 <Route path='/' component={HomePage} />
 <Route path='/about' component={About} />
</Switch>


来源:https://stackoverflow.com/questions/44872941/does-react-router-v4-supports-dynamic-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!