我使用免费的管理模板和配置的路由和工作正常,但当我尝试使用移动的侧边栏折叠不工作.如何解决这个在reactjs?

4sup72z8  于 2023-03-22  发布在  React
关注(0)|答案(1)|浏览(80)

![How to to dissapear sidebar view after click Link located within sidebar][I need to collapse this sidebar Link after click in React JS and Router-dom]

import { useState } from 'react';
import { Link } from 'react-router-dom'

const SideBar = () => {
const [open, setOpen] = useState(true);

const onOpen = () => {
setOpen(!open);
};
 return (
 <div className="main-menu menu-fixed menu-light menu-accordion    menu-shadow " data-scroll-to-active="true" data-img="theme-assets/images/backgrounds/02.jpg">
<div className="navbar-header">
  <ul className="nav navbar-nav flex-row">       
    <li className="nav-item mr-auto">
      <Link to="/" className="navbar-brand" onClick={onOpen}>
          <img className="brand-logo" alt="Chameleon admin logo" src="theme-assets/images/logo/logo.png"/>
          <h3 className="brand-text">LaravelTricks</h3>
      </Link>
    </li>
  </ul>
</div>
<div className="main-menu-content">
  <ul className="navigation navigation-main" id="main-menu-navigation" data-menu="menu-navigation">
    <li className="active"><Link to="/"><i className="ft-home"></i><span className="menu-title" data-i18n="">Dashboard</span></Link>
    </li>
    <li className=" nav-item"><Link to="/to-do-list"><i className="ft-pie-chart"></i><span className="menu-title" data-i18n="">Tasks</span></Link>
    </li>
    <li className=" nav-item"><Link to="/"><i className="la la-sign-out"></i><span className="menu-title" data-i18n="">Logout</span></Link>
    </li>
  </ul>
</div>
<div className="navigation-background"></div>

); }export default SideBar;

z9gpfhce

z9gpfhce1#

要在单击位于侧边栏中的链接后折叠侧边栏,您需要在单击链接时调用的onOpen func中将open的状态更新为false。以下是更新后的代码:

import { useState } from 'react';
import { Link } from 'react-router-dom';

const SideBar = () => {
  const [open, setOpen] = useState(true);

  const onOpen = () => {
    setOpen(false);
  };

  return (
    <div className={`main-menu menu-fixed menu-light menu-accordion menu-shadow ${open ? '' : 'menu-collapsed'}`} data-scroll-to-active="true" data-img="theme-assets/images/backgrounds/02.jpg">
      <div className="navbar-header">
        <ul className="nav navbar-nav flex-row">       
          <li className="nav-item mr-auto">
            <Link to="/" className="navbar-brand" onClick={onOpen}>
              <img className="brand-logo" alt="Chameleon admin logo" src="theme-assets/images/logo/logo.png"/>
              <h3 className="brand-text">LaravelTricks</h3>
            </Link>
          </li>
        </ul>
      </div>
      <div className="main-menu-content">
        <ul className="navigation navigation-main" id="main-menu-navigation" data-menu="menu-navigation">
          <li className="active"><Link to="/"><i className="ft-home"></i><span className="menu-title" data-i18n="">Dashboard</span></Link></li>
          <li className="nav-item"><Link to="/to-do-list" onClick={onOpen}><i className="ft-pie-chart"></i><span className="menu-title" data-i18n="">Tasks</span></Link></li>
          <li className="nav-item"><Link to="/" onClick={onOpen}><i className="la la-sign-out"></i><span className="menu-title" data-i18n="">Logout</span></Link></li>
        </ul>
      </div>
      <div className="navigation-background"></div>
    </div>
  );
};

export default SideBar;

相关问题