简体   繁体   English

如何确定当前路由在 React 中是否处于活动状态

[英]How to determine if the current route is active in React

react v0.14.7 / react-router: v2.0.1反应 v0.14.7 /反应路由器:v2.0.1

I have my Routes set up as follows:我的路线设置如下:

import { Router, Route, IndexRoute, hashHistory } from "react-router"

ReactDOM.render(
  <Router history={hashHistory}>
    <Route path="/" component={App}>
      <IndexRoute component={Index}/>
      <Route path="/locations" component={Locations}/>
      <Route path="/products" component={Products}/>
    </Route>
  </Router>, 
  document.getElementById('app'));

I'm trying to write a method in one of my React classes (created using the following API)我正在尝试在我的一个 React 类中编写一个方法(使用以下 API 创建)

export default class MyClass extends React.Component {...}

...that determines if the current URL's path is a particular route's path. ...确定当前 URL 的路径是否是特定路由的路径。

For example.例如。 Assuming the current URL is http://domain/products then calling:假设当前 URL 是http://domain/products然后调用:

isActive('/products')

would return TRUE会返回TRUE

I'm aware of React Router's Link object activeClassName .我知道 React Router 的Link对象activeClassName But I need to write something that looks this up programmatically in a different location to where I defined the <link/> objects.但是我需要编写一些东西,在与我定义<link/>对象的位置不同的位置以编程方式查找它。

Looking in node_modules/react-router/ I can see modules/isActive.js that does what I need, however it's not export ed in modules/index.js , so i'm assuming I cannot access it?查看node_modules/react-router/我可以看到modules/isActive.js node_modules/react-router/我的需要,但是它没有在modules/index.js export ,所以我假设我无法访问它?

I also see from the API docs that there is an RouterContext isActive method , but again, I don't know how to call this?我还从 API 文档中看到有一个 RouterContext isActive 方法,但同样,我不知道如何调用它?

OK thanks to Mike 'Pomax' Kamermans, I resolved the issue like so:好的,感谢 Mike 'Pomax' Kamermans,我像这样解决了这个问题:

Changed create class API from:将创建类 API 更改为:

export default class MyClass extends React.Component {

    ...  

}

to:到:

export default React.createClass({
    ...
})

And added the following contextTypes definition to the class:并将以下contextTypes定义添加到类中:

export default React.createClass({

  contextTypes: {
    router: React.PropTypes.object
  },

  render(){

    console.log(this.context.router.isActive('/foo')); // Now works :)

    ...
  }

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Angular 中,您如何确定活动路线? - In Angular, how do you determine the active route? 如何使用 react-dom-router 处理活动路由/路由更改 - How to handle active route/route change with react-dom-router 如何确定选项卡是否是给定浏览器窗口的活动(当前)选项卡? - How to determine if a tab is the active (current) tab of a given browser window? 如何仅在当前路线上挂起“活动”班级? - How can I hang the “active” class only on the current route? 如何在react-router 1中获得活动的路由路径名? - How do I get the active route pathname in react-router 1? 如果链接指向当前活动的“页面”,如何禁用React链接和功能 - How to disable React link and function if link points to current active “page” 如何在 react-router 2.0.0-rc5 中获取当前路由 - How to get current route in react-router 2.0.0-rc5 我如何知道我在 react-navigation 5 中的当前路线? - How can I know my current route in react-navigation 5? React Native:如何在Navigator的navigationBar = {}中引用当前路由? - React Native: How to reference current route in Navigator's navigationBar={}? 如何在没有道具的情况下从反应中获得当前路线 - How to get the current route from react without props
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM