【问题标题】:React Router with a custom root and a base component具有自定义根和基本组件的 React Router
【发布时间】:2026-01-22 05:10:01
【问题描述】:

我有一个集成的 React 应用程序(它已集成到现有的 Flask 应用程序中),并且 React 应用程序的入口不在站点的根目录中。触发 React 应用程序的第一个 URL 是“/active-items”。

但是,其他路线不一定延伸该路径。有些路线是'/active-items/item/',而有些则完全不同,例如'/item-selection'。

考虑到这一点,我正在尝试设置我的 React 路由器,以便为每个路由提供一个基础组件。基本上,任何触发的路由都应该将“App”组件作为基础组件,并且在 App 组件中我有“{props.children}”。

我已经尝试了几次路线的外观,但没有运气。

我的最新版本是这样的:

<Router>
  <div>
    <Route path='/' component={App}>
      <Route path='active-items' component={ActiveItems} />
    </Route>
  </div>
</Router>

App 已渲染,但 ActiveItems 未渲染。有什么想法我应该如何处理这个问题?

编辑:我正在使用 react-router-dom v4.0.0

【问题讨论】:

  • 你为什么要用 div 组件包装你的路由?你使用的是什么版本的 React Router?
  • 嵌套路由意味着您将渲染它们嵌套,因此在您的情况下,您必须有一个用于“/”的路由和另一个用于“/active-items”的路由。看看React-router docs。并删除 div 标签
  • @RafaelBerro 我正在使用“react-router-dom”:“^4.0.0”。那里有一个 div,因为有一次我尝试将它们并排渲染,除非它们被包装,否则它无法工作。我遵循了本教程:reacttraining.com/react-router/web/guides/quick-start
  • 您应该阅读official documentation 并检查更改。你不能再用这种方式定义嵌套路由了。
  • @FacundoLaRocca 我浏览了文档(链接在您链接的 github 页面内),并且在他们的示例中也有一个 div。

标签: reactjs react-router react-router-dom


【解决方案1】:

原答案

React Router 的第 4 版有许多重大更改。您不能再以这种方式使用嵌套路由。查看这个使用新版本的 React Router 的嵌套路由示例。

const Header = () => (
  <nav>
    <ul>
      <li>Home</li>
      <li>About</li>
    </ul>
  </nav>
)

const Footer = () => (
  <footer>
    Copyrights
  </footer>
)

const Layout = props => (
  <div className="layout">
    <div className="layout__container">
      <Header />{ props.children }
      <Footer />
    </div>
  </div>
)

const Home = () => <Layout>This is the home page.</Layout>
const About = () => <Layout>This is the about page.</Layout>

<Router>
  <div>
    <Route path="/" component={Home} />
    <Route path="/about" component={About} />
  </div>
</Router>

希望对你有帮助。

更新答案(11 月 16 日)

这就是我实际上正在做的事情。

import { BrowserRouter, Route, Switch } from 'react-router-dom'
import React from 'react'

import About from './components/views/About'
import Home from './components/views/Home'
import Layout from './components/views/Layout'

const Routes = () => (
  <BrowserRouter>
    <Layout>
      <Switch>
        <Route exact path='/' component={Home} />
        <Route exact path='/about' component={About} />
      </Switch>
    </Layout>
  </BrowserRouter>
)

export default Routes

【讨论】:

  • 酷,现在让我试一试
  • 我已经更新了答案,请再看一遍。
  • 我明白了,您正在路线本身之外进行嵌套。让我现在试试。
  • 这也不错。
【解决方案2】:

我认为您误解了 React-router 的工作方式。

嵌套路由呈现为“嵌套”,这意味着父路由将呈现自身,然后呈现所有子路由,通常用于公共路由。

这里有一个我正在使用的工作示例:

<Router history={browserHistory}>
  <Route path='/' component={Root}>
    <Route path='/examples/react-redux-websocket' component={App} />
    <Route path='/examples/react-redux-websocket/chat/:chatid/participant/:participantid' component={Chat} />
    <Route path='/examples/react-redux-websocket/chat/:chatid' component={Chat} />
    <Route path='/*' component={NoMatch} />
  </Route>
</Router>

class Root extends Component {
  render () {
    return (
      <div>
        {this.props.children}
        <Footer />
      </div>
    )
  }
}

如您所见,根路由渲染一个公共的页脚,然后渲染所有嵌套的子级。 Root 内的所有路由也将呈现相同的页脚。

我认为你需要的是这样的:

<Router>
  <Route path='/' component={App} />
  <Route path='/active-items' component={ActiveItems} />
</Router>

查看 www.facundolarocca.com 以查看它的工作原理,您也会找到 repo。

让我知道它是否有效。

【讨论】:

  • 这是我最初尝试的方法 - 它确实渲染了两个组件,但 ActiveItems 是在整个 App 组件之后渲染的 - 即使在页脚之后(我也有一个页脚),因为它没有嵌套。
  • 好的,您介意分享组件吗?应用程序和 ActiveItems
  • 请参阅上面接受的答案 - 我使用的是 v4,它引入了重大更改。我一直在尝试像你在这里写的东西,但是在新版本中它不再起作用了。
  • 是的,我看到了,很高兴知道,我喜欢跟上时代。谢谢
最近更新 更多