超全模态模型 × Harness 升级,升级解锁 ArkClaw,最新支持 DeepSeek-V4 系列与 GLM-5.1
activeKey
"home"
key
defaultActiveKey
href
useState
仅需修改
import React, { useState } from 'react'; import { Nav, NavDropdown } from 'react-bootstrap'; import 'bootstrap/dist/css/bootstrap.min.css'; export default function NavSample() { const [key, setKey] = useState('home'); const handleSelect = (key) => { setKey(key); }; // 仅修改Nav组件的属性,删除冗余的defaultActiveKey、把固定的activeKey替换为状态变量 return ( <Nav variant="pills" activeKey={key} onSelect={handleSelect}> <Nav.Item> <Nav.Link eventKey="home" href="/home"> Home </Nav.Link> </Nav.Item> <Nav.Item> <Nav.Link eventKey="about" href="/about" title="about"> About </Nav.Link> </Nav.Item> <Nav.Item> <Nav.Link eventKey="contact" href="/contact">Contact</Nav.Link> </Nav.Item> <NavDropdown title="Pricing" id="nav-dropdown"> <NavDropdown.Item eventKey="Basic pricing" href="/pricing/basic pricing">Basic</NavDropdown.Item> <NavDropdown.Item eventKey="Corporate Pricing" href="/pricing/corporate pricing">Corporates</NavDropdown.Item> <NavDropdown.Divider /> <NavDropdown.Item eventKey="Enterprise Pricing" href="/pricing/enterprise pricing">Enterprise</NavDropdown.Item> </NavDropdown> </Nav> ); }
如果你的项目是React单页应用,推荐配合React Router实现无刷新跳转,同时可以直接通过当前路径自动匹配激活项,不需要手动维护选中状态:
import React from 'react'; import { Nav, NavDropdown } from 'react-bootstrap'; import { Link, useLocation } from 'react-router-dom'; import 'bootstrap/dist/css/bootstrap.min.css'; export default function NavSample() { // 自动获取当前路径 const location = useLocation(); // 路径和eventKey的映射关系,可根据实际路由规则调整 const getActiveKey = () => { const path = location.pathname; if(path === '/home') return 'home'; if(path === '/about') return 'about'; if(path === '/contact') return 'contact'; if(path.includes('/pricing')) return path.split('/')[2]; return 'home'; }; return ( <Nav variant="pills" activeKey={getActiveKey()}> <Nav.Item> <Nav.Link as={Link} eventKey="home" to="/home"> Home </Nav.Link> </Nav.Item> <Nav.Item> <Nav.Link as={Link} eventKey="about" to="/about" title="about"> About </Nav.Link> </Nav.Item> <Nav.Item> <Nav.Link as={Link} eventKey="contact" to="/contact">Contact</Nav.Link> </Nav.Item> <NavDropdown title="Pricing" id="nav-dropdown"> <NavDropdown.Item as={Link} eventKey="Basic pricing" to="/pricing/basic pricing">Basic</NavDropdown.Item> <NavDropdown.Item as={Link} eventKey="Corporate Pricing" to="/pricing/corporate pricing">Corporates</NavDropdown.Item> <NavDropdown.Divider /> <NavDropdown.Item as={Link} eventKey="Enterprise Pricing" to="/pricing/enterprise pricing">Enterprise</NavDropdown.Item> </NavDropdown> </Nav> ); }
如果是多页应用场景,建议在组件初始化时通过window.location.pathname匹配对应的激活key作为useState的初始值,避免页面刷新后高亮重置。
window.location.pathname
内容的提问来源于stack exchange,提问作者Rafael
超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起
模型再升级,30秒超长叙事, 模态参考扩容
模型自由,工具不限,最新支持 Deepseek-V4 系列、GLM-5.3 系列
超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列
大模型19元起,Al应用9.9元畅享,新人首购爆款尽享优惠