I'm trying to get the current path of the react router in a container so I can pass it to a child component that will change it's visibility filter.
More specifically, I'm trying to make a navigation menu highlight the currently active page.
I'm using react, redux, react-router, and react-router-redux so I can access the router state from the redux store.
From the docs for react-router-redux, it says to do something like this:
function mapStateToProps(state, ownProps) {
return {
id: ownProps.params.id,
filter: ownProps.location.query.filter
};
}
Here is my container component:
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import {
Segment as UISegment,
} from 'semantic-ui-react'
import NavMenu from '../components/NavMenu'
class MenuBar extends Component {
static propTypes = {
path: PropTypes.string.isRequired
}
render() {
const { path, } = this.props
return (
<UISegment>
<NavMenu activePath={path} />
</UISegment>
)
}
}
const mapStateToProps = (state, ownProps) => {
return {
path: ownProps.route ? ownProps.route.path : "/"
}
}
export default connect(mapStateToProps)(MenuBar)
Inside the NavMenu component, a semantic-ui menu component will compare activePath
with its own path and highlight the active button.
Everything seems to work in theory; when I click on the different parts of the menu, a @@router/LOCATION_CHANGE
action is emitted. In the redux dev tools, I see the state changing. However, mapStateToProps
is never called and this component is never re-rendered.
Any ideas? I thought about using the react methods like shouldComponentUpdate
, but it seems that react doesn't even realize the state or props are changing.