Skip to content
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

connect prevents LinkContainer from marking links as active #388

Closed
olalonde opened this issue May 22, 2016 · 9 comments
Closed

connect prevents LinkContainer from marking links as active #388

olalonde opened this issue May 22, 2016 · 9 comments

Comments

@olalonde
Copy link

Took me a while to figure this one out. For some strange reason, when I use connect to decorate my component, <LinkContainer> stops marking links as active. If I remove the connect()(Header) call, <LinkContainer> works as expected.

import React from 'react'
import { connect } from 'react-redux'
import { LinkContainer } from 'react-router-bootstrap'
import {
  Nav,
  Navbar,
  NavItem,
} from 'react-bootstrap'

class Header extends React.Component {
  render() {
    return (
      <div>
        <Navbar>
          <Navbar.Collapse>
            <Nav pullLeft>
              <LinkContainer to="/dashboard/registrations">
                <NavItem>
                  Registrations
                </NavItem>
              </LinkContainer>
              <LinkContainer to="/dashboard/monitoring">
                <NavItem>
                  Monitoring
                </NavItem>
              </LinkContainer>
            </Nav>
          </Navbar.Collapse>
        </Navbar>
      </div>
    )
  }
}

export default Header

/*
When using connect(), LinkContainer stops working and
links are no longer marked as active when URL changes
*/

// export default connect()(Header)

Here's the behaviour I get when using connect:

gif

Thinks those are related but I can't figure out a quick/official fix:

remix-run/react-router#3286
remix-run/react-router#470

@gaearon
Copy link
Contributor

gaearon commented May 22, 2016

This is fixed in React Router 3.0.0-alpha.1 and newer.

@gaearon gaearon closed this as completed May 22, 2016
@gaearon
Copy link
Contributor

gaearon commented May 22, 2016

In fact the last comment in the issue you linked says exactly that 😉
remix-run/react-router#470 (comment)

@olalonde
Copy link
Author

olalonde commented May 22, 2016

@gaearon thanks. Upgrading to React Router 3.0.0-alpha.1 doesn't appear to have fixed the problem :(

Got this warning though:

npm WARN [email protected] requires a peer of react-router@>=2.0.0 but none was installed.

@olalonde
Copy link
Author

Just tested with <Link> directly and same problem.

@olalonde
Copy link
Author

export default connect(s => ({
  user: s.user,
}))(withRouter(Header))

fixed the problem.

@gaearon
Copy link
Contributor

gaearon commented May 23, 2016

The Link component is fixed in router 3.0 alpha 1. Not the LinkContainer component from the router bootstrap package. Unfortunately it doesn't even use the Link directly so it doesn't benefit from the Link being fixed. I will file an issue there.

@olalonde
Copy link
Author

@gaearon ah ok thanks. Any idea why withRouter() fixed the problem anyway?

@gaearon
Copy link
Contributor

gaearon commented May 23, 2016

@olalonde Because both withRouter() and Link contain the necessary fix.

@gaearon
Copy link
Contributor

gaearon commented May 23, 2016

FWIW you can use withRouter(LinkContainer) as a temporary measure.

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

2 participants