0

I have a component with header and sidebar which wraps the routers of my application, the problem I'm having is that <NavLink> are not updating on the route change assuming that the cause of this problem is that they are above the router and hence not affected by the change, i tried every way possible but no solution, how can i make the <NavLink> add the active class to the current route ?

here is my 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>

my 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>

how can i update this component so the <NavLink> keep on adding the 'active' class to the right item in the sidebar ?

Abdul-Elah JS
  • 586
  • 10
  • 31

2 Answers2

1

You need to wrap the connect with the withRouter and not the other way around (see the important note at https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/withRouter.md).

So instead of

export default connect(state_to_props)(withRouter(PrivateRoute));

try

export default withRouter(connect(state_to_props)(PrivateRoute));
Gabriele Petrioli
  • 173,972
  • 30
  • 239
  • 291
0

your sidebar menu file u can export with 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);
Asif vora
  • 1,117
  • 2
  • 8
  • 19