reactjs - React Router v4 - 如何获取当前路由?

标签 reactjs react-router react-router-v4

我想显示title<AppBar />它是以某种方式从当前路由传入的。

在 React Router v4 中, <AppBar /> 会怎样?能够将当前路由传递到它的 title支柱?

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

最佳答案

在react-router 5.1版本中,有一个名为useLocation的钩子(Hook),它返回当前位置对象。当您需要知道当前 URL 时,这可能很有用。

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

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

关于reactjs - React Router v4 - 如何获取当前路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42253277/

相关文章:

javascript - 如何将本 map 片链接到js中的字符串

typescript - 使用 react 路由器,即使参数存在也无法提取参数(使用 typescript )

reactjs - 如何更改页面的全部内容

react-router - 如何防止使用react-router v4匹配两条路由?

reactjs - 使用 render() 时 REACT 路由器无法访问参数

reactjs - 如何在 React 中处理来自 D3 的大量 DOM 操作以利用其 virtualDOM

reactjs - Ant Design - 复选框的垂直组

css - 如何用 CSS 包裹 React Js 组件

javascript - React-Router:链接更新 URL 但 View 不变

reactjs - 获取 react-router v4 中 pathHistory 的当前索引