HiveBrain v1.2.0
Get Started
← Back to all entries
snippetreactCritical

React Router v4 - How to get current route?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
routegetcurrentreactrouterhow

Problem

I'd like to display a title in ` that is somehow passed in from the current route.

In React Router v4, how would
be able to get the current route passed into it's title prop?


    
      
      
      
      
    
  


Is there a way to pass a custom title from a custom
prop on `?

Solution

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 Path : {location.pathname}
}

Code Snippets

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

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

Context

Stack Overflow Q#42253277, score: 523

Revisions (0)

No revisions yet.