NavLink 仅在单击时更改按钮颜色但不进行设置

NavLink change button color only on click moment but not setting it up

我对 Navlink 按钮的活动有问题 class 我的代码如下所示:

<NavLink exact to="/"><Button>Page</Button></NavLink>

NavLink isActive 不知何故无法正常工作。只有当我点击按钮时,它才会将 class 变为活动状态,但在我释放按钮后它又不再活动。

按钮样式组件:

import styled from 'styled-components';

const Button = styled.button`
  width: 50%;
  height:35px;
  background: white;
  color: #71C1A1;
  padding: 0;
  border:none;

   &:active {
      background: #71C1A1;
      color: white;
    }
`;

export default Button;

也许有人可以帮忙?

react router给元素<NavLink />的标准class是active(可以用activeClassName="customClass"控制给定的class)。

因此,您需要使用 class .active 为包裹在 link 中的按钮设置样式。像这样:

const Button = styled.button`

   /* ... your other styles */

  .active & {
      background: #71c1a1;
      color: white;
  }

`;