我有一个带有标题和侧边栏的组件,该组件包装了我的应用程序的路由器,我遇到的问题是<NavLink>
不会在路由更改上进行更新,假定此问题的原因是在路由器上方,因此不受更改的影响,我尝试了所有可能的方法,但没有解决方案,如何使<NavLink>
类active
添加到当前路由?
这是我的app.js
<Switch>
<Route path="/login" component={ Login } />
<PrivateRoute path="/" component={ Dashboard } exact />
<PrivateRoute path="/members" component={ Members } exact />
<PrivateRoute path="/payments" component={ Bills } exact />
<PrivateRoute path="/gas-stations" component={ GasStations } exact />
<PrivateRoute path="/transactions" component={ Transaction } exact />
<PrivateRoute path="/cms" component={ CMS } />
<PrivateRoute path="/members/:member_id" component={ Member } />
<PrivateRoute path="/gas-stations/:gs" component={ GS } />
<PrivateRoute path="/transactions/:transaction_id" component={ FoundTransaction } />
<PrivateRoute path="/payments/:payment_id" component={ Payment } />
</Switch>
我的private-routes.js
import React from 'react';
import { Route, Redirect, withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
import Container from 'components/container';
const PrivateRoute = ({ component : Component, authenticated, ...rest }) => {
return (
<Route { ...rest } render={ (props) => (
authenticated === true
? <Container> // this is the component that wraps the header and the sidebar
<Component { ...props } />
</Container>
: <Redirect to="/login" />
) } />
)
}
const state_to_props = (state, prop) => {
return {
authenticated : state.authenticated
}
}
export default connect(state_to_props)(withRouter(PrivateRoute));
container.js
return (
<div>
<Header />
<SideMenu />
<div className="app-content">
{ props.children }
</div>
</div>
)
side-menu.js
<li>
<NavLink to="/" exact> <span>Dashboard</span> </NavLink>
</li>
<li>
<NavLink exact to="/members"> <span>Members</span> </NavLink>
</li>
<li>
<NavLink exact to="/gas-stations"> <span>Gas Stations</span> </NavLink>
</li>
<li>
<NavLink exact to="/payments"> <span>Payments</span> </NavLink>
</li>
<li>
<NavLink exact to="/transactions"> <span>Transactions</span> </NavLink>
</li>
<li>
<NavLink exact to="/cms"> <span>CMS</span> </NavLink>
</li>
我如何更新此组件,以便<NavLink>
继续在边栏中的正确项目上添加'active'类?
答案 0 :(得分:1)
您需要将connect
用withRouter
包裹起来,而不要反过来(请参阅https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/withRouter.md上的重要说明 >)。
所以不是
export default connect(state_to_props)(withRouter(PrivateRoute));
尝试
export default withRouter(connect(state_to_props)(PrivateRoute));
答案 1 :(得分:0)
您的侧边栏菜单文件可以通过withRouter()
导出。
import React, { Component } from "react";
import { Link, withRouter } from 'react-router-dom';
class Sidebar extends Component {
constructor(){
....
}
render(){
return(
<li>
<NavLink to="/" exact> <span>Dashboard</span> </NavLink>
</li>
<li>
<NavLink exact to="/members"> <span>Members</span> </NavLink>
</li>
<li>
<NavLink exact to="/gas-stations"> <span>Gas Stations</span> </NavLink>
</li>
<li>
<NavLink exact to="/payments"> <span>Payments</span> </NavLink>
</li>
<li>
<NavLink exact to="/transactions"> <span>Transactions</span> </NavLink>
</li>
<li>
<NavLink exact to="/cms"> <span>CMS</span> </NavLink>
</li>
)
}
}
export default withRouter(Sidebar);