我有一个链接列表
<NavLink className="item" activeClassName="item-active" to="/url?filter=1">filter 1</NavLink>
<NavLink className="item" activeClassName="item-active" to="/url?filter=2">filter 2</NavLink>
<NavLink className="item" activeClassName="item-active" to="/url?filter=3">filter 3</NavLink>
我的问题是所有链接都具有项目有效的类名。有没有办法使其更具体?谢谢
答案 0 :(得分:0)
使用isActive
属性指定何时启用网址。
const isUrlActive = (match, location) =>
location.pathname + location.search === `/url${search}`;
<NavLink
className="item"
activeClassName="item-active"
to="/url?filter=1"
isActive={isUrlActive}
>filter 1</NavLink>
<NavLink
className="item"
activeClassName="item-active"
to="/url?filter=2"
isActive={isUrlActive}
>filter 2</NavLink>
<NavLink
className="item"
activeClassName="item-active"
to="/url?filter=3"
isActive={isUrlActive}
>filter 3</NavLink>
参考:https://github.com/ReactTraining/react-router/issues/5379
答案 1 :(得分:0)
NavLink documentation中有两个可能有用的道具: