I am using V4 React-Router-DOM
I need to access this.props.matchin my component of the navigation bar so that I can set the active class. I use reactive materialization. In the tag <NavItem>I want to add className={this.props.match ? 'active' : ''}. However, I cannot access the match. Props is an empty object every time 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 the details I missed & the details transferred from the reaction router
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 on the Internet are for v2 and 3. The documents for v4 do not contain details on how to handle this. Many people have enclosed a route for their application. However, when I do this, I get a stack overflow in the console with frame printing.
, ?