简体   繁体   English

当我单击 react.js 中的链接时折叠导航栏

[英]Collapsing the navbar when i click on link in react.js

well as you can see this is an ordinary navbar code, what i'm trying to do is just when the user clicks on the any links/buttons inside the <Navbar.Collapse id="navbar"> it should collapse, Thanks in advance!正如您所看到的,这是一个普通的导航栏代码,我想要做的只是当用户点击<Navbar.Collapse id="navbar">内的任何链接/按钮时它应该崩溃,提前致谢!

<Navbar bg="light" expand="md">
                <Container fluid>
                    <Navbar.Brand href="/">
                        <LOGO />
                    </Navbar.Brand>
                    <Navbar.Toggle aria-controls="navbar">
                        {/* <FaBars /> */}
                    </Navbar.Toggle>
                    <Navbar.Collapse id="navbar">
                        <Nav className="my-3 my-md-auto">
                            <NavLink
                                exact="true"
                                activeclassname="active"
                                to="/"
                                className="rounded-circle nav-link"
                            >
                                <FaHome />
                                <span className="rounded-pill d-md-none px-md-3 ms-2">
                                    Home
                                </span>
                            </NavLink>
                            <NavLink
                                exact="true"
                                activeclassname="active"
                                className="rounded-circle nav-link"
                                to="/Blog"
                            >
                                <FaBlog />
                                <span className="rounded-pill d-md-none px-md-3 ms-2">
                                    Blog
                                </span>
                            </NavLink>
                            <NavLink
                                exact="true"
                                activeclassname="active"
                                className="rounded-circle nav-link"
                                to="/Portfolio"
                            >
                                <FaFolder />
                                <span className="rounded-pill d-md-none px-md-3 ms-2">
                                    Portfolio
                                </span>
                            </NavLink>
                            <NavLink
                                exact="true"
                                activeclassname="active"
                                className="rounded-circle nav-link"
                                to="/About"
                            >
                                <FaExclamationCircle />
                                <span className="rounded-pill d-md-none px-md-3 ms-2">
                                    About
                                </span>
                            </NavLink>
                            <NavLink
                                exact="true"
                                activeclassname="active"
                                className="rounded-circle nav-link"
                                to="/Contact"
                            >
                                <FaEnvelope />
                                <span className="rounded-pill d-md-none px-md-3 ms-2">
                                    Contact
                                </span>
                            </NavLink>
                        </Nav>
                    </Navbar.Collapse>
                </Container>
            </Navbar>

Listen to the router change event with the useLocation Hook from react-router-dom使用 react-router-dom 中的 useLocation Hook 监听路由器更改事件

import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';

function SomeComponent() {
  const location = useLocation();

  useEffect(() => {
    console.log('Location changed');
    // change the navbar view state here
  }, [location]);

  //Rest of the code
}

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

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