简体   繁体   English

具有Reactstrap和React Router Dom的主动NavLink

[英]Active NavLink with Reactstrap and React Router Dom

I'm building the navbar component for my application but I'm having problems with the activeClassName prop. 我正在为我的应用程序构建navbar组件,但是activeClassName属性存在问题。 It doesn't change any class or style when i change between the Links. 当我在链接之间进行更改时,它不会更改任何类或样式。 I've tried everything I saw on Internet with no results. 我已经尝试了在互联网上看到的所有内容,但都没有结果。 Maybe someone can give me an advice with this issue. 也许有人可以给我一个关于这个问题的建议。

My imports: 我的进口:

import React from 'react';
import { NavLink as RRNavLink } from 'react-router-dom';
import { Nav, NavItem, NavLink } from 'reactstrap';

This is the code I have so far: 这是我到目前为止的代码:

    <Nav className="navbar-logged">
      <NavItem>
        <NavLink
          className="nav-link-gdc"
          activeClassName="active"
          to="/home"
          tag={RRNavLink}
        >
            INICIO
        </NavLink>
      </NavItem>
      <NavItem>
        <NavLink
          className="nav-link-gdc"
          activeClassName="active"
          to="/secondLink"
          tag={RRNavLink}
        >
            secondLink
        </NavLink>
      </NavItem>
      <NavItem>
        <NavLink
          className="nav-link-gdc"
          activeClassName="active"
          to="/thirdLink"
          tag={RRNavLink}
        >
            thirdLink
        </NavLink>
      </NavItem>
    </Nav>

Thank You 谢谢

A possible reason is that you are using Redux to manage application's state 一个可能的原因是您正在使用Redux来管理应用程序的状态

https://github.com/reactjs/react-redux/blob/master/docs/troubleshooting.md#my-views-arent-updating-when-something-changes-outside-of-redux https://github.com/reactjs/react-redux/blob/master/docs/troubleshooting.md#my-views-arent-updating-when-something-changes-outside-of-redux

If your views depend on global state or React “context”, you might find that views decorated with connect() will fail to update. 如果您的视图依赖于全局状态或React“上下文”,则可能会发现用connect()装饰的视图将无法更新。

This is because connect() implements shouldComponentUpdate by default, assuming that your component will produce the same results given the same props and state. 这是因为connect()默认情况下实现了shouldComponentUpdate,假设在给定相同props和state的情况下,您的组件将产生相同的结果。 This is a similar concept to React's PureRenderMixin. 这与React的PureRenderMixin类似。

The fastest ( not the best ) solution for this is to pass the pure: false option to connect() function of the component that has your NavLink(s) (in my case it's the Header) 最快( 不是最好 )的解决方案是将具有NavLink的组件的pure(:false)选项传递给connect()函数(在我的情况下是Header)

function mapStateToProps(state) {
  return { test: state.test}
}

export default connect(mapStateToProps, null, null, {
  pure: false
})(Header)

This will remove the assumption that Header is pure and cause it to update whenever its parent component renders. 这将消除Header是纯净的假设,并使其在其父组件呈现时进行更新。 Note that this will make your application less performant, so only do this if you have no other option. 请注意,这会使您的应用程序性能降低,因此只有在没有其他选择的情况下才这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM