devwannabe
devwannabe

Reputation: 3210

Accessing this.props.location.pathname within the Route component

On my SideMenuNavigation component where I have the <Route> codes, I need to access the value of this.props.location.pathname. Unfortunately, I'm getting undefined.

class SideNavigation extends React.Component {

  componentDidMount() {
    console.log(this.props.location.pathname); // undefined
  }

  render() {
    return(
     <Router>
       <Route exact path="/" component={Dashboard}/>
     </Router>
    )
  }
}


/*   */

In my Dashboard component, I can acess this.props.location.pathname successfully. Unforunately, I don't need it from this component. I need the value in the SideMenuNavigation component.

Upvotes: 0

Views: 3751

Answers (1)

Alexander Reshytko
Alexander Reshytko

Reputation: 2236

location is one of the props that are injected by Router component. In your case SideNavigation isn't Router component's child (in fact it is a parent). You're might be interested in this question also: How does react-router pass params to other components via props?

Upvotes: 1

Related Questions