Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide collapse navbar after link click, react-redux, react-bootstrap

I'm beginner in react, so, need your help. I used collapsible bootstrap navbar, have no ideas how to make it collapse after link click on mobile. Native bootsrap property collapseOnSelect not working, or i did something wrong.

const  AppBar = () => (
  <Navbar collapseOnSelect>
    <Navbar.Header>
      <Navbar.Brand>
        <div className="logo-wrap"> 
          <Link to="define"> 
            <img height='50' src='./../assets/img/logo.png' className="logo"/> 
          </Link> 
        </div> 
      </Navbar.Brand>
      <Navbar.Toggle />
    </Navbar.Header>
    <Navbar.Collapse>
      <Nav>
        <LinkContainer to="define" className="nav-link"><NavItem eventKey={1}>Home</NavItem></LinkContainer>
        <LinkContainer to="about" className="nav-link"><NavItem eventKey={1}>About</NavItem></LinkContainer>
        <LinkContainer to="features" className="nav-link"><NavItem eventKey={1}>Features</NavItem></LinkContainer>
        <LinkContainer to="pricing" className="nav-link"><NavItem eventKey={1}>Pricing</NavItem></LinkContainer>
        <LinkContainer to="areaMap" className="nav-link"><NavItem eventKey={1}>Area Map</NavItem></LinkContainer>
      </Nav>
      <Nav pullRight>        
        <LinkContainer to="login" className="nav-link"><NavItem eventKey={2}>Log In</NavItem></LinkContainer>
        <LinkContainer to="registration"><NavItem eventKey={2}><Button className="sign-up">Sign Up</Button></NavItem></LinkContainer>
      </Nav>
    </Navbar.Collapse>
  </Navbar>
);


export default AppBar;
like image 725
Andrew Avatar asked Nov 19 '22 12:11

Andrew


1 Answers

const [expanded, setExpanded] = useState(false);

Second in the Navbar we add this prop:

<Navbar expanded={expanded}>

Third, we add an onClick event to the toggle handler that changes the menu visibility status:

<Navbar.Toggle onClick={() => setExpanded(expanded ? false : "expanded")} />

Fourth we add the prop

<Link to="/" onClick={() => setExpanded(false)}>Menu</Link>
like image 172
Pervez Avatar answered Dec 11 '22 03:12

Pervez