我正在将react router添加到现有项目中.

目前,一个模型被传递到一个根组件,其中包含一个用于子导航的导航组件和一个主组件.

我发现的react路由示例只有一个子组件,在不重复两个组件中的布局代码的情况下更改多个子组件的最佳方法是什么?

推荐答案

如果我理解正确,要实现这一点,你需要在Route中定义多个组件.你可以像这样使用它:

// think of it outside the context of the router, if you had pluggable
// portions of your `render`, you might do it like this
<App children={{main: <Users/>, sidebar: <UsersSidebar/>}}/>

// So with the router it looks like this:
const routes = (
  <Route component={App}>
    <Route path="groups" components={{main: Groups, sidebar: GroupsSidebar}}/>
    <Route path="users" components={{main: Users, sidebar: UsersSidebar}}>
      <Route path="users/:userId" component={Profile}/>
    </Route>
  </Route>
)

class App extends React.Component {
  render () {
    const { main, sidebar } = this.props;
    return (
      <div>
        <div className="Main">
          {main}
        </div>
        <div className="Sidebar">
          {sidebar}
        </div>
      </div>
    )
  }
}

class Users extends React.Component {
  render () {
    return (
      <div>
        {/* if at "/users/123" `children` will be <Profile> */}
        {/* UsersSidebar will also get <Profile> as this.props.children,
            so its a little weird, but you can decide which one wants
            to continue with the nesting */}
        {this.props.children}
      </div>
    )
  }
}

另外,看看sidebar example app,应该会对你有更多帮助.

Edit:

在最新版本的路由(v3)中,组件位于props对象的根目录中

所以:

const { main, sidebar } = this.props.children;

变成:

const { main, sidebar } = this.props;

EDIT:

import React from 'react'
import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'

// Each logical "route" has two components, one for
// the sidebar and one for the main area. We want to
// render both of them in different places when the
// path matches the current URL.
const routes = [
  { path: '/',
    exact: true,
    sidebar: () => <div>home!</div>,
    main: () => <h2>Home</h2>
  },
  { path: '/bubblegum',
    sidebar: () => <div>bubblegum!</div>,
    main: () => <h2>Bubblegum</h2>
  },
  { path: '/shoelaces',
    sidebar: () => <div>shoelaces!</div>,
    main: () => <h2>Shoelaces</h2>
  }
]

const SidebarExample = () => (
  <Router>
    <div style={{ display: 'flex' }}>
      <div style={{
        padding: '10px',
        width: '40%',
        background: '#f0f0f0'
      }}>
        <ul style={{ listStyleType: 'none', padding: 0 }}>
          <li><Link to="/">Home</Link></li>
          <li><Link to="/bubblegum">Bubblegum</Link></li>
          <li><Link to="/shoelaces">Shoelaces</Link></li>
        </ul>

        {routes.map((route, index) => (
          // You can render a <Route> in as many places
          // as you want in your app. It will render along
          // with any other <Route>s that also match the URL.
          // So, a sidebar or breadcrumbs or anything else
          // that requires you to render multiple things
          // in multiple places at the same URL is nothing
          // more than multiple <Route>s.
          <Route
            key={index}
            path={route.path}
            exact={route.exact}
            component={route.sidebar}
          />
        ))}
      </div>

      <div style={{ flex: 1, padding: '10px' }}>
        {routes.map((route, index) => (
          // Render more <Route>s with the same paths as
          // above, but different components this time.
          <Route
            key={index}
            path={route.path}
            exact={route.exact}
            component={route.main}
          />
        ))}
      </div>
    </div>
  </Router>
)

export default SidebarExample

请务必在此处查看新的React Router v4文档:https://reacttraining.com/react-router/

Reactjs相关问答推荐

重定向React路由中没有根文件夹的所有路径匹配

Reaction Native:在初始获取后,Reducer变为未定义

有没有办法将在服务器端全局获取的一些数据传递到Next&>13应用程序目录中的客户端组件?

在Map()完成React.js中的多个垃圾API请求后执行函数

GoLang有条件地为.js.gz提供服务(如果它存在),否则为.js

无法在REACTION TANSTACK查询中传递参数

useEffect不重新渲染

如何更新 FunctionComponentElement 的 props

如何管理组件列表的复杂状态? (Nextjs/ReactJS)

Mui 在 TextField 标签上添加工具提示显示两个工具提示框

在一个事件处理程序中进行多次调度是个好主意吗?

臭名昭著的测试未包含在 act(...) 中

React - 错误边界未捕获错误

将 useState 设置为组件内部的值

将路径数组传递给Route会引发错误

setState 改变对象引用

使用 React 中的功能组件更改另一个组件的状态

当状态改变时如何执行一些动作,但只针对第一次更新?

如果查询不存在,如何返回所有产品?

为什么我不能使用 formik 更改此嵌套对象中的值