React Router V4 Get Current Path
You will only have access to this.props.location
in a Route
component (as the prop is passed to the component from it).
You can get the current path with window.location.pathname
outside of the Router
, though it's likely that you are doing something wrong if you are accessing the path here.
For children of Router
, you can access the location by wrapping it around withRouter
or pass the prop downwards from a Route
component.
Redux integration will also allow you to access the location from anywhere.
Fast solution
Create a variable on your function:
var currentLocation = window.location.pathname;
and then in the ul > li className={(currentLocation == '/' ? 'active' : '')}
it worked for mee and is a quick solution.