0

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.

devwannabe
  • 3,160
  • 8
  • 42
  • 79

1 Answers1

1

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?

Community
  • 1
  • 1
Alexander Reshytko
  • 2,126
  • 1
  • 20
  • 28
  • I got it working with the use of withRouter and pathless so `export default withRouter(SideNavigation)` and It's fixed! :) – devwannabe Feb 27 '17 at 04:18