Skip to content

LinkContainer is setting active class twice and active class not getting removed on brand link click #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jainvizz opened this issue Dec 13, 2018 · 6 comments

Comments

@jainvizz
Copy link

I am using react bootstrap with react-router to implement nav bar functionality. Below I am providing the code for the same.

Routes

<Switch>
          <Route exact path="/" component={Home}/>
          <Route exact path="/categoryType" component={CategoryType}/>
          <Route exact path="/category" component={Category}/>
          <Redirect from='*' to='/' />
        </Switch>


Nav Links

<Navbar bg="dark" expand="lg" variant="dark">
        <Navbar.Brand><Link className="navbar-brand" to="/">Project Blog</Link></Navbar.Brand>
        <Navbar.Toggle aria-controls="navbarCollapse" />
        <Navbar.Collapse id="navbarCollapse">
          <Nav className="mr-auto">
            <Nav.Item><IndexLinkContainer to={"/"}><Nav.Link>Home</Nav.Link></IndexLinkContainer></Nav.Item>
            <Nav.Item><LinkContainer to={"/categoryType"}><Nav.Link>Category Type</Nav.Link></LinkContainer></Nav.Item>
            <Nav.Item><LinkContainer to={"/category"}><Nav.Link>Category</Nav.Link></LinkContainer></Nav.Item>
          </Nav>
        </Navbar.Collapse>
      </Navbar>

Everything is working but I am getting one weird issue, when I select a menu item it is getting selected but when I click on brand link it is selecting Home page which is correct but not de-selecting already selected menu.

I created the code sandbox for the same.

https://codesandbox.io/s/l4978989m9

Can any one please help me out with this.

Thanks,
Rahul

@nickboucart
Copy link

I'm seeing the same behaviour, any progress on this?

@jainvizz
Copy link
Author

jainvizz commented Feb 5, 2019

@nickboucart No I am still not able to find any solution for this.

@manonthemoon42
Copy link

Facing the exact same problem. Unfortunate that the class active doesn't get removed automatically.

@jainvizz
Copy link
Author

For the time being I switched back to NavLink (react-router-dom)

<Navbar bg="dark" expand="lg" variant="dark">
        <Navbar.Brand><Link className="navbar-brand" to="/">Project Blog</Link></Navbar.Brand>
        <Navbar.Toggle aria-controls="navbarCollapse" />
        <Navbar.Collapse id="navbarCollapse">
          <Nav className="mr-auto">
            <Nav.Item><NavLink className="nav-link" exact to={"/"}>Home</NavLink></Nav.Item>
            <Nav.Item><NavLink className="nav-link" exact to={"/categoryType"}>Category Type</NavLink></Nav.Item>
            <Nav.Item><NavLink className="nav-link" exact to={"/category"}>Category</NavLink></Nav.Item>
          </Nav>
        </Navbar.Collapse>
      </Navbar>

May be it can help someone.

@thejohnfreeman
Copy link

Note: @jainvizz's solution works only when the NavLink is rendered on every route change.

@v12
Copy link
Member

v12 commented Apr 5, 2019

Closing this one as a duplicate of #242.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants