React Router v4 - How to get current route? - reactjs

I'd like to display a title in <AppBar /> that is somehow passed in from the current route.
In React Router v4, how would <AppBar /> be able to get the current route passed into it's title prop?
<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>
Is there a way to pass a custom title from a custom prop on <Route />?

In the 5.1 release of react-router there is a hook called useLocation, which returns the current location object. This might useful any time you need to know the current URL.
import { useLocation } from 'react-router-dom'
function HeaderView() {
const location = useLocation();
console.log(location.pathname);
return <span>Path : {location.pathname}</span>
}

In react router 4 the current route is in -
this.props.location.pathname.
Just get this.props and verify.
If you still do not see location.pathname then you should use the decorator withRouter.
This might look something like this:
import {withRouter} from 'react-router-dom';
const SomeComponent = withRouter(props => <MyComponent {...props}/>);
class MyComponent extends React.Component {
SomeMethod () {
const {pathname} = this.props.location;
}
}

If you are using react's templates, where the end of your react file looks like this: export default SomeComponent you need to use the higher-order component (often referred to as an "HOC"), withRouter.
First, you'll need to import withRouter like so:
import {withRouter} from 'react-router-dom';
Next, you'll want to use withRouter. You can do this by change your component's export. It's likely you want to change from export default ComponentName to export default withRouter(ComponentName).
Then you can get the route (and other information) from props. Specifically, location, match, and history. The code to spit out the pathname would be:
console.log(this.props.location.pathname);
A good writeup with additional information is available here: https://reacttraining.com/react-router/core/guides/philosophy

There's a hook called useLocation in react-router v5, no need for HOC or other stuff, it's very succinctly and convenient.
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>
);
}

Here is a solution using history Read more
import { createBrowserHistory } from "history";
const history = createBrowserHistory()
inside Router
<Router>
{history.location.pathname}
</Router>

Has Con Posidielov said, the current route is present in this.props.location.pathname.
But if you want to match a more specific field like a key (or a name), you may use matchPath to find the original route reference.
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}`)

I think the author's of React Router (v4) just added that withRouter HOC to appease certain users. However, I believe the better approach is to just use render prop and make a simple PropsRoute component that passes those props. This is easier to test as you it doesn't "connect" the component like withRouter does. Have a bunch of nested components wrapped in withRouter and it's not going to be fun. Another benefit is you can also use this pass through whatever props you want to the Route. Here's the simple example using render prop. (pretty much the exact example from their website 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;
usage: (notice to get the route params (match.params) you can just use this component and those will be passed for you)
import React from 'react';
import PropsRoute from 'src/components/routes/props-route';
export const someComponent = props => (<PropsRoute component={ Profile } />);
also notice that you could pass whatever extra props you want this way too
<PropsRoute isFetching={ isFetchingProfile } title="User Profile" component={ Profile } />

Add
import {withRouter} from 'react-router-dom';
Then change your component export
export default withRouter(ComponentName)
Then you can access the route directly within the component itself (without touching anything else in your project) using:
window.location.pathname
Tested March 2020 with: "version": "5.1.2"

Related

react-router-dom get id from route with custom components and extra path

I have an app that uses a react-router-config and uses a wrapper component to redirect unauthenticated users.
I have some functionality that requires the use of the route /tasks/:id but I am unable to access the :id value to perform the necessary task lookup.
My routes.js:
import React from "react";
...
const Tasks = React.lazy(() => import("./Components/Tasks"));
...
const routes = [
{
path: "/tasks/edit/:id",
name: "View Task",
component: Tasks
}
...
];
export default routes;
Then I have AuthenticatedRoute.js:
import React from "react";
import { Route, Redirect } from "react-router-dom";
export default function AuthenticatedRoute({
component: C,
appProps,
...rest
}) {
return (
<Route
{...rest}
render={props =>
appProps.isAuthenticated ? (
<C {...props} {...appProps} />
) : (
<Redirect
to={`/login?redirect=${props.location.pathname}${props.location.search}`}
/>
)
}
/>
);
}
and in the App.js:
import React, { useState, useEffect } from "react";
import { BrowserRouter, Switch, withRouter } from "react-router-dom";
import AuthenticatedRoute from "./components/AuthenticatedRoute/AuthenticatedRoute";
import routes from "./routes";
function App(props) {
...
return (
<BrowserRouter>
<React.Suspense fallback={loading()}>
<Switch>
{routes.map((route, idx) => {
return route.component ? (
<AuthenticatedRoute
key={idx}
path={route.path}
exact={route.exact}
name={route.name}
appProps={props}
component={props => <route.component {...props} />}
/>
) : null;
})}
</Switch>
...
</React.Suspense>
</BrowserRouter>
Finally I have my Tasks.js:
import React, { useState, useEffect } from "react";
...
function Tasks(props) {
useEffect(() => {
onLoad();
}, []);
const onLoad = async () => {
console.log(JSON.stringify(props.match.params));
};
...
Browsing to localhost:3000/tasks/1. props.match.params is empty in every component up the chain. props.match.params.id is undefined. I have also tried match with {match.params.id} but that is also undefined at every component.
I can see props.location.pathname but that is the full path and I would have to manually get the last segment. I can't get it to automatically grab the :id from the url.
EDIT
Turns out my example was too simplified, which actually helped me identify the issue. In my previous version when I had the route:
{
path: "/tasks/:id",
name: "View Task",
component: Tasks
}
everything actually works fine using useParams I am able to get the :id value. What I actually have in my app and what seems to be breaking it is adding an additional directory to the path:
{
path: "/tasks/edit/:id",
name: "View Task",
component: Tasks
}
I am not sure how this makes a difference, but having the extra /edit seems to break the useParams
react-router-dom provides some handy hooks. In your case, I'd suggest useParams() (link to docs)
import { useParams } from 'react-router-dom';
function MyComponent {
let { id } = useParams();
return (<p>{id}</p>);
}
I'd also probably opt for not using withRouter if you will be using the hooks provided by React Router
Martins answer is correct and works for functional components.
I had the same problem, however I was using a class component (class Company extends Component) and Martins answer did not work for that case.
If you have a class component you can do:
import { withRouter } from 'react-router-dom'
class MyComponent extends React.Component {
componentDidMount() {
// here you have the id
const id = this.props.match.params.id;
}
}
export default withRouter(MyComponent);

Why is match missing from props?

I'm writing my first react application and I wanna use a Switch to show the component corresponding to the route. One of the routes uses a param. The problem is that the match attribute is missing from props so route matching doesn't seem to work (No component is inserted).
When I try to console.log this.props.match it returns undefined.
import { Switch, Route } from 'react-router-dom';
import React, { Component } from 'react';
import Index from './Index';
import Debate from './Debate';
class App extends Component {
componentDidMount() {
console.log(this.props.match); // I get undefined
}
render() {
return (
<div className="App">
<Switch>
<Route path="/debats" component={Index} />
<Route path="/debat/:debateSlug" component={Debate} />
</Switch>
</div>
);
}
}
export default withConfig(App);
I want to be able to access the match attribute so that the correct component is displayed.
Add to this in your Component
import {withRouter} from 'react-router-dom';
export default withRouter(withConfig(App));

type error cannot read property 'push' of undefined reactjs [duplicate]

I am developing an application in which I check if the user is not loggedIn. I have to display the login form, else dispatch an action that would change the route and load other component. Here is my code:
render() {
if (isLoggedIn) {
// dispatch an action to change the route
}
// return login component
<Login />
}
How can I achieve this as I cannot change states inside the render function.
Considering you are using react-router v4
Use your component with withRouter and use history.push from props to change the route. You need to make use of withRouter only when your component is not receiving the Router props, this may happen in cases when your component is a nested child of a component rendered by the Router and you haven't passed the Router props to it or when the component is not linked to the Router at all and is rendered as a separate component from the Routes.
import {withRouter} from 'react-router';
class App extends React.Component {
...
componenDidMount() {
// get isLoggedIn from localStorage or API call
if (isLoggedIn) {
// dispatch an action to change the route
this.props.history.push('/home');
}
}
render() {
// return login component
return <Login />
}
}
export default withRouter(App);
Important Note
If you are using withRouter to prevent updates from being blocked by
shouldComponentUpdate, it is important that withRouter wraps the
component that implements shouldComponentUpdate. For example, when
using Redux:
// This gets around shouldComponentUpdate
withRouter(connect(...)(MyComponent))
// This does not
connect(...)(withRouter(MyComponent))
or you could use Redirect
import {withRouter} from 'react-router';
class App extends React.Component {
...
render() {
if(isLoggedIn) {
return <Redirect to="/home"/>
}
// return login component
return <Login />
}
}
With react-router v2 or react-router v3, you can make use of context to dynamically change the route like
class App extends React.Component {
...
render() {
if (isLoggedIn) {
// dispatch an action to change the route
this.context.router.push('/home');
}
// return login component
return <Login />
}
}
App.contextTypes = {
router: React.PropTypes.object.isRequired
}
export default App;
or use
import { browserHistory } from 'react-router';
browserHistory.push('/some/path');
In react-router version 4:
import React from 'react'
import { BrowserRouter as Router, Route, Redirect} from 'react-router-dom'
const Example = () => (
if (isLoggedIn) {
<OtherComponent />
} else {
<Router>
<Redirect push to="/login" />
<Route path="/login" component={Login}/>
</Router>
}
)
const Login = () => (
<h1>Form Components</h1>
...
)
export default Example;
Another alternative is to handle this using Thunk-style asynchronous actions (which are safe/allowed to have side-effects).
If you use Thunk, you can inject the same history object into both your <Router> component and Thunk actions using thunk.withExtraArgument, like this:
import React from 'react'
import { BrowserRouter as Router, Route, Redirect} from 'react-router-dom'
import { createBrowserHistory } from "history"
import { applyMiddleware, createStore } from "redux"
import thunk from "redux-thunk"
const history = createBrowserHistory()
const middlewares = applyMiddleware(thunk.withExtraArgument({history}))
const store = createStore(appReducer, middlewares)
render(
<Provider store={store}
<Router history={history}>
<Route path="*" component={CatchAll} />
</Router
</Provider>,
appDiv)
Then in your action-creators, you will have a history instance that is safe to use with ReactRouter, so you can just trigger a regular Redux event if you're not logged in:
// meanwhile... in action-creators.js
export const notLoggedIn = () => {
return (dispatch, getState, {history}) => {
history.push(`/login`)
}
}
Another advantage of this is that the url is easier to handle, now, so we can put redirect info on the query string, etc.
You can try still doing this check in your Render methods, but if it causes problems, you might consider doing it in componentDidMount, or elsewhere in the lifecycle (although also I understand the desire to stick with Stateless Functional Compeonents!)
You can still use Redux and mapDispatchToProps to inject the action creator into your comptonent, so your component is still only loosely connected to Redux.
This is my handle loggedIn. react-router v4
PrivateRoute is allow enter path if user is loggedIn and save the token to localStorge
function PrivateRoute({ component: Component, ...rest }) {
return (
<Route
{...rest}
render={props => (localStorage.token) ? <Component {...props} /> : (
<Redirect
to={{
pathname: '/signin',
state: { from: props.location },
}}
/>
)
}
/>
);
}
Define all paths in your app in here
export default (
<main>
<Switch>
<Route exact path="/signin" component={SignIn} />
<Route exact path="/signup" component={SignUp} />
<PrivateRoute path="/" component={Home} />
</Switch>
</main>
);
Those who are facing issues in implementing this on react-router v4. Here is a working solution for navigating through the react app programmatically.
history.js
import createHistory from 'history/createBrowserHistory'
export default createHistory()
App.js OR Route.jsx. Pass history as a prop to your Router.
import { Router, Route } from 'react-router-dom'
import history from './history'
...
<Router history={history}>
<Route path="/test" component={Test}/>
</Router>
You can use push() to navigate.
import history from './history'
...
render() {
if (isLoggedIn) {
history.push('/test') // this should change the url and re-render Test component
}
// return login component
<Login />
}
All thanks to this comment: https://github.com/ReactTraining/react-router/issues/3498#issuecomment-301057248
render(){
return (
<div>
{ this.props.redirect ? <Redirect to="/" /> :'' }
<div>
add here component codes
</div>
</div>
);
}
I would suggest you to use connected-react-router https://github.com/supasate/connected-react-router
which helps to perform navigation even from reducers/actions if you want.
it is well documented and easy to configure
I was able to use history within stateless functional component, using withRouter following way (needed to ignore typescript warning):
import { withRouter } from 'react-router-dom';
...
type Props = { myProp: boolean };
// #ts-ignore
export const MyComponent: FC<Props> = withRouter(({ myProp, history }) => {
...
})
import { useNavigate } from "react-router-dom"; //with v6
export default function Component() {
const navigate = useNavigate();
navigate.push('/path');
}
I had this issue and just solved it with the new useNavigate hook in version 6 of react-router-dom

React Router NavLink not triggering history.listen() function

I am trying to implement Google Analytics using the react-ga library which needs to fire whenever the route changes. I have the following setup in App.js:
import createBrowserHistory from 'history/createBrowserHistory';
...
const history = createBrowserHistory();
history.listen((location) => {
console.log('ANALYTICS CODE GOES HERE', location);
});
class App extends Component {
render() {
return (
<Provider store={...}>
<Router history={history}>
....
</Router>
</Provider>
);
}
}
export default App;
When I click on a NavLink throughout my app, the route changes and loads the appropriate component, but the history.listen() function never fires. However, if I use the browser's back/forward functionality it does fire.
<NavLink to="/account" activeClassName="active">ACCOUNT</NavLink>
How do I listen to route changes triggered by a NavLink?
Edit: further discussion taking place here: https://github.com/react-ga/react-ga/issues/122#issuecomment-316427527
Use a react-router <Route /> component as a wrapper for your tracking function - the route re-renders every time the location changes. For example:
import React from 'react'
import { BrowserRouter as Router, Route } from 'react-router-dom'
import ReactGA from 'react-ga'
ReactGA.initialize('UA-00000000-1')
const tracker = ({location}) => {
// console.log('tracking', location.pathname)
ReactGA.set({page: location.pathname})
ReactGA.pageview(location.pathname)
return null
}
const App = (props) => (
<Router>
<div>
<Route render={tracker} />
...
</div>
</Router>
)

How can i access the current hash location on react router 2?

I want to access the current location path (like /home) without the history keys for some comparison logic.
How can i access it from the hashHistory in react router 2.
Also, how can i access the previous path?
You can obtain the current pathname from the location prop of your route component.
Access location from this.props.location of your Route component. If
you'd like to get it deeper in the tree, you can use whatever
conventions your app has for getting props from high down low. One
option is to provide it on context yourself:
// v2.0.x const RouteComponent = React.createClass({
childContextTypes: {
location: React.PropTypes.object },
getChildContext() {
return { location: this.props.location } } })
Have a look here.
To get the current path, you can just use location.pathname.
One way to access path is via this.props.location from a react component.
import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router, Route, Link } from 'react-router'
class Child extends React.Component {
render() {
let location = this.props.location
return (
<div>
<h1>Child Component</h1>
<p>{location.pathname}</p>
</div>
)
}
}
class App extends React.Component {
render() {
return (
<div>
{this.props.children}
</div>
)
}
}
render((
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="child" component={Child} />
</Route>
</Router>
), document.getElementById('example'))
Another way if you want listen to changes in current location via history -
import { createHistory } from 'history'
let history = createHistory()
// Listen for changes to the current location. The
// listener is called once immediately.
let unlisten = history.listen(function (location) {
console.log(location.pathname)
})
// When you're finished, stop the listener.
unlisten()

Resources