338

我想显示一个title<AppBar />某种方式从当前路由传入的 in。

在 React Router v4 中,如何将<AppBar />当前路由传递到它的titleprop 中?

  <Router basename='/app'>
    <main>
      <Menu active={menu} close={this.closeMenu} />
      <Overlay active={menu} onClick={this.closeMenu} />
      <AppBar handleMenuIcon={this.handleMenuIcon} title='Test' />
      <Route path='/customers' component={Customers} />
    </main>
  </Router>

prop有没有办法从自定义传递自定义标题<Route />

4

8 回答 8

361

在 react-router 的 5.1 版本中有一个叫做useLocation的钩子,它返回当前的位置对象。这在您需要知道当前 URL 的任何时候都可能有用。

import { useLocation } from 'react-router-dom'

function HeaderView() {
  const location = useLocation();
  console.log(location.pathname);
  return <span>Path : {location.pathname}</span>
}

于 2020-03-18T09:35:52.000 回答
244

在反应路由器 4 中,当前路由位于 - 中 this.props.location.pathname。只需获取 this.props并验证。如果您仍然看不到 location.pathname,那么您应该使用装饰器withRouter

这可能看起来像这样:

import {withRouter} from 'react-router-dom';

const SomeComponent = withRouter(props => <MyComponent {...props}/>);

class MyComponent extends React.Component {
  SomeMethod () {
    const {pathname} = this.props.location;
  }
}
于 2017-05-11T06:52:40.290 回答
87

如果您使用的是 react 的模板,那么您的 react 文件的结尾如下所示:export default SomeComponent您需要使用高阶组件(通常称为“HOC”)withRouter,.

首先,您需要withRouter像这样导入:

import {withRouter} from 'react-router-dom';

接下来,您将要使用 withRouter. 您可以通过更改组件的导出来做到这一点。您可能想从 更改export default ComponentNameexport default withRouter(ComponentName)

然后你可以从道具中获取路线(和其他信息)。具体来说,locationmatchhistory。吐出路径名的代码是:

console.log(this.props.location.pathname);

此处提供了带有附加信息的好文章:https ://reacttraining.com/react-router/core/guides/philosophy

于 2018-07-17T20:22:15.753 回答
69

react-router v5中有一个叫useLocation的钩子,不需要HOC之类的东西,非常简洁方便。

import { useLocation } from 'react-router-dom';

const ExampleComponent: React.FC = () => {
  const location = useLocation();  

  return (
    <Router basename='/app'>
      <main>
        <AppBar handleMenuIcon={this.handleMenuIcon} title={location.pathname} />
      </main>
    </Router>
  );
}
于 2020-02-20T04:13:50.243 回答
33

这是使用history 阅读更多的解决方案

import { createBrowserHistory } from "history";

const history = createBrowserHistory()

内部路由器

<Router>
   {history.location.pathname}
</Router>
于 2019-09-13T22:01:59.900 回答
25

Con Posdielov说过,当前路线存在于this.props.location.pathname.

但是,如果您想匹配更具体的字段,例如键(或名称),您可以使用matchPath来查找原始路由引用。

import { matchPath } from `react-router`

const routes = [{
  key: 'page1'
  exact: true,
  path: '/page1/:someparam/',
  component: Page1,
},
{
  exact: true,
  key: 'page2',
  path: '/page2',
  component: Page2,
},
]

const currentRoute = routes.find(
  route => matchPath(this.props.location.pathname, route)
)

console.log(`My current route key is : ${currentRoute.key}`)
于 2019-12-06T17:07:36.567 回答
11

我认为 React Router (v4) 的作者只是添加了 withRouter HOC 来安抚某些用户。但是,我相信更好的方法是只使用渲染道具并制作一个简单的 PropsRoute 组件来传递这些道具。这更容易测试,因为它不像 withRouter 那样“连接”组件。将一堆嵌套组件包裹在 withRouter 中,这不会很有趣。另一个好处是你也可以通过你想要的任何道具来使用这个通道。这是使用渲染道具的简单示例。(几乎是他们网站https://reacttraining.com/react-router/web/api/Route/render-func的确切示例)(src/components/routes/props-route)

import React from 'react';
import { Route } from 'react-router';

export const PropsRoute = ({ component: Component, ...props }) => (
  <Route
    { ...props }
    render={ renderProps => (<Component { ...renderProps } { ...props } />) }
  />
);

export default PropsRoute;

用法:(注意获取路由参数(match.params)你可以只使用这个组件,这些将被传递给你)

import React from 'react';
import PropsRoute from 'src/components/routes/props-route';

export const someComponent = props => (<PropsRoute component={ Profile } />);

还请注意,您也可以通过这种方式传递您想要的任何额外道具

<PropsRoute isFetching={ isFetchingProfile } title="User Profile" component={ Profile } />
于 2018-05-14T16:51:23.383 回答
1

添加

import {withRouter} from 'react-router-dom';

然后更改您的组件导出

export default withRouter(ComponentName)

然后,您可以使用以下方法直接在组件本身内访问路由(无需触及项目中的任何其他内容):

window.location.pathname

2020 年 3 月测试:“版本”:“5.1.2”

于 2020-03-18T09:25:05.140 回答