How can I access the current route from anywhere in my app?


im1dermike

I'm trying to get information about the current route on a non-Route component.

React Router provides singleton versions of history (browserHistory and hashHistory) that you can import and use from anywhere in your application.

Previously, you seemed to be able to use browserHistoryit, but it doesn't seem to be supported anymore. I am using react-router-redux^4.0.8and @types\react-router-redux^5.0.1.

How to access current route location?

João Cunha

this is very simple. You can use withRouter HOC

You can access the properties of the history object and the closest Route match via the withRouter higher-order component . Whenever rendered, withRouter will pass the updated match, position and history props to the wrapped component.

like to use it like this:

import {withRouter} from "react-router-dom"
@withRouter
class NonRouteComponent extends Component {
  render() {
    // you have access to this.props.match
    // you have access to this.props.history
    // you have access to this.props.location
    const { match, location, history } = this.props

    return <h1>Hi {location.pathname}</h1>;
  }
}

If you don't use the decorator (aka @withRouter), you can export the component like this:

class NonRouteComponent extends Component { ... }
export default withRouter(NonRouteComponent);

Related


If my API is on a different "route", how can I access it?

jf1234 Let's say my current page is at: http://myserver/client/index.html My API is at url: http://myserver/api/getMyData How can I access the API without hardcoding it like this: fetch('http://myserver/api/getMyData') instead like this: fetch('../api/getMyDa

How can I load an image and access it from anywhere?

Tyler Myers I want to browse images from form window. I also created a class and created some filters. I can read this image from the form. My goal is to announce it in my class. and use this image anywhere. But I don't know how I should do it. private void bt

How can I access the current route from anywhere in my app?

im1dermike I'm trying to get information about the current route on a non-Route component. React Router provides singleton versions of history (browserHistory and hashHistory) that you can import and use from anywhere in your application. Previously, you seeme

If my API is on a different "route", how can I access it?

jf1234 Let's say my current page is at: http://myserver/client/index.html My API is at url: http://myserver/api/getMyData How can I access the API without hardcoding it like this: fetch('http://myserver/api/getMyData') instead like this: fetch('../api/getMyDa

How can I access my ubuntu server from anywhere in the world?

Christian Azzam How To Remotely Access Ubuntu Server 18.04.1 Now I have setup Ubuntu server 18.04.1 and access it from my laptop using puTTY. I want to be able to access it via PuTTY or ssh or whatever in the world. Do I need a port forwarding router to access

How can I load an image and access it from anywhere?

Tyler Myers I want to browse images from form window. I also created a class and created some filters. I can read this image from the form. My goal is to announce it in my class. and use this image anywhere. But I don't know how I should do it. private void bt

How can I access my ubuntu server from anywhere in the world?

Christian Azzam How To Remotely Access Ubuntu Server 18.04.1 Now I have setup Ubuntu server 18.04.1 and access it from my laptop using puTTY. I want to be able to access it via PuTTY or ssh or whatever in the world. Do I need a port forwarding router to access

How can I access my app from a custom ArrayAdapter?

Tim Sim How I usually access my application: MyApplication myApp = (MyApplication)this.getApplication(); But this cannot be public class MyArrayAdapter extends ArrayAdapter<SomeObject> {} Because getApplication() is an activity method. Is there a way to acce

How can I access my app from a custom ArrayAdapter?

Tim Sim How I usually access my application: MyApplication myApp = (MyApplication)this.getApplication(); But this cannot be public class MyArrayAdapter extends ArrayAdapter<SomeObject> {} Because getApplication() is an activity method. Is there a way to acce

How can I access the current route from anywhere in my app?

im1dermike I'm trying to get information about the current route on a non-Route component. React Router provides singleton versions of history (browserHistory and hashHistory) that you can import and use from anywhere in your application. Previously, you seeme

How can I access the current route from anywhere in my app?

im1dermike I'm trying to get information about the current route on a non-Route component. React Router provides singleton versions of history (browserHistory and hashHistory) that you can import and use from anywhere in your application. Previously, you seeme

How can I access my ubuntu server from anywhere in the world?

Christian Azzam How To Remotely Access Ubuntu Server 18.04.1 Now I have setup Ubuntu server 18.04.1 and access it from my laptop using puTTY. I want to be able to access it via PuTTY or ssh or whatever in the world. Do I need a port forwarding router to access

How can I access my ubuntu server from anywhere in the world?

Christian Azzam How To Remotely Access Ubuntu Server 18.04.1 Now I have setup Ubuntu server 18.04.1 and access it from my laptop using puTTY. I want to be able to access it via PuTTY or ssh or whatever in the world. Do I need a port forwarding router to access

How can I access my app from a custom ArrayAdapter?

Tim Sim How I usually access my application: MyApplication myApp = (MyApplication)this.getApplication(); But this cannot be public class MyArrayAdapter extends ArrayAdapter<SomeObject> {} Because getApplication() is an activity method. Is there a way to acce