I am using v4 of react-router-dom
I need to access this.props.match
in my nav bar component so I can set the active class. I'm using react-materialize. In the <NavItem>
tag, I want to add className={this.props.match ? 'active' : ''}
. However, I can't seem to access the match. Props is an empty object every time when I print it in the console.
My Nav.js
<Navbar brand='Fuzic' href="/" className="orange darken-3" right>
<NavItem className={this.match ? 'active' : ''} href="/devices">Devices</NavItem>
<NavItem><Link to="/devices">Media</Link></NavItem>
<NavItem><Link to="/devices">Alarms</Link></NavItem>
<NavItem><Link to="/devices">Interrupts</Link></NavItem>
<NavItem><Link to="/auth">Admin</Link></NavItem>
</Navbar>
My App.js
<BrowserRouter>
<div>
<Nav/>
<div className="container">
<Switch>
<PropsRoute exact path="/" component={Auth}/>
<PropsRoute exact path="/devices" component={Devices} devices={this.state.devices} setCurrentDevice={this.setCurrentDevice} />
<PropsRoute path="/devices/:deviceId" component={Detail} currentDevice={this.state.currentDevice} />
<PropsRoute path="/auth" component={Auth}/>
<PropsRoute component={NotFound}/>
</Switch>
</div>
</div>
</BrowserRouter>
helper.js - combines props passed by me & props passed from react-router
// Exerp From: https://github.com/ReactTraining/react-router/issues/4105
export const renderMergedProps = (component, ...rest) => {
const finalProps = Object.assign({}, ...rest);
return (
React.createElement(component, finalProps)
);
}
export const PropsRoute = ({ component, ...rest }) => {
return (
<Route {...rest} render={routeProps => {
return renderMergedProps(component, routeProps, rest);
}}/>
);
}
Most of the documentation and articles online are for v2&3. The docs for v4 don't go into detail on how to handle this. Many people nested a route for their app. However, when I do that, I get a stack overflow in the console with a print of the frames.
How do I fix this so I can get access to match?