Reactjs React路由器v4-如何获取当前路由?

Reactjs React路由器v4-如何获取当前路由?,reactjs,react-router,react-router-v4,Reactjs,React Router,React Router V4,我想在中显示一个标题,它是以某种方式从当前路线传入的 在React Router v4中,如何将当前路由传递到其标题道具中 <Router basename='/app'> <main> <Menu active={menu} close={this.closeMenu} /> <Overlay active={menu} onClick={this.closeMenu} /> <AppBar

我想在
中显示一个
标题
,它是以某种方式从当前路线传入的

在React Router v4中,
如何将当前路由传递到其
标题
道具中

  <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>


是否有方法从
上的自定义
道具传递自定义标题?

在react路由器4中,当前路由位于-
此.props.location.pathname
。 只需获取
这个.props
并进行验证。 如果仍然看不到
location.pathname
,则应使用路由器的装饰程序

这可能看起来像这样:

从“react router dom”导入{withRouter};
const SomeComponent=withRouter(props=>);
类MyComponent扩展了React.Component{
方法(){
const{pathname}=this.props.location;
}
}

我认为React Router(v4)的作者刚刚添加了With Router HOC以安抚某些用户。但是,我认为更好的方法是只使用渲染道具,并制作一个简单的道具路由组件来传递这些道具。这更容易测试,因为它不像withRouter那样“连接”组件。用路由器包装一堆嵌套的组件,这将不是一件有趣的事。另一个好处是你也可以使用这个道具通过你想要的路线。下面是使用渲染道具的简单示例。(和他们网站上的例子差不多) (src/组件/路线/道具路线)

从“React”导入React;
从“反应路由器”导入{Route};
export const PropsRoute=({component:component,…props})=>(
() }
/>
);
导出默认的PropsRoute;
用法:(请注意,要获取路由参数(match.params),您只需使用此组件,这些组件将为您传递)

从“React”导入React;
从“src/components/routes/props route”导入PropsRoute;
export const someComponent=props=>();
还要注意的是,你也可以通过这种方式传递任何你想要的额外道具

<PropsRoute isFetching={ isFetchingProfile } title="User Profile" component={ Profile } />

如果您使用的是react的模板,react文件的结尾如下:
导出默认SomeComponent
您需要使用高阶组件(通常称为“HOC”),
with Router

首先,您需要使用路由器导入
,如下所示:

import {withRouter} from 'react-router-dom';
接下来,您将要使用路由器的
。可以通过更改组件的导出来完成此操作。您可能希望从
export default ComponentName
更改为
export default with router(ComponentName)

然后你可以从道具上获得路线(和其他信息)。具体来说,
位置
匹配
,以及
历史
。吐出路径名的代码是:

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

这里提供了一个包含附加信息的好的书写工具:

这里有一个使用
历史记录的解决方案

内部路由器

<Router>
   {history.location.pathname}
</Router>

{history.location.pathname}
已经说过,当前路由存在于
这个.props.location.pathname

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

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}`)
在react router v5中有一个称为hook的钩子,不需要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>
  );
}
从'react router dom'导入{useLocation};
常量示例组件:React.FC=()=>{
const location=useLocation();
返回(
);
}
添加

然后更改组件导出

export default withRouter(ComponentName)
然后,您可以使用以下方式直接在组件内部访问管线(而不接触项目中的任何其他内容):


于2020年3月测试:“版本”:“5.1.2”在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>
}

从'react router dom'导入{useLocation}
函数HeaderView(){
const location=useLocation();
console.log(location.pathname);
返回路径:{location.pathname}
}

请注意,这并不总是正确的:对于react-router4,如果您的组件在某些嵌套路由的更高级别上呈现(稍后会扩展路径)WithRouter内部的location.pathname将不同于
window.location.pathname
我们如何在React组件外部以编程方式获取它?此解决方案不提供如何在持有自身的组件中使用这些道具。是否有可能复制此解决方案,我们可以将其转换为代码笔?我想使用更正确的解决方案,但我真的不知道如何实现。对于我们这些处于react功能路径的人来说,这个答案最有意义。那些
this.props….
任何对我们来说只是噪音的东西。这是唯一一个直接在标签内部工作的解决方案,没有子组件,也没有直接使用文档/窗口位置。但是如何获得完整的url?这个答案的评级应该更高。使用钩子比这些其他解决方案(如果在RR v5上)更简单、更直观。这是截至RR v5的当前答案。对此,我不确定。当然,它给出了当前路径名,但如果我导航到新页面,这不会改变。请注意,这只能在路由器DOM中使用(即chid组件,而不是在使用路由器的类/功能组件中)。这可能很明显,但对像我这样的新手来说并非如此:-)我一直收到错误“useContext is undefined”,如果使用路由器
重定向
,这也将不起作用。这将读取重定向之前的路径,为什么不将位置设为
const
而不是
let
,因为我们知道它在这种情况下不会改变?这对于单元测试兼容性非常好,避免模拟风
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>
  );
}
import {withRouter} from 'react-router-dom';
export default withRouter(ComponentName)
window.location.pathname
import { useLocation } from 'react-router-dom'

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