简体   繁体   English

带有React和Bootstrap的可折叠边栏

[英]Collapsible Sidebar with React and Bootstrap

I want to make a collapsable sidebar using Bootstrap and React and I'm following this tutorial. 我想使用Bootstrap和React创建一个可折叠的侧边栏,我正在学习本教程。 http://seegatesite.com/create-simple-cool-sidebar-menu-with-bootstrap-3/ http://seegatesite.com/create-simple-cool-sidebar-menu-with-bootstrap-3/

I was able to generate a static site, but I can't get the jquery script to work with React. 我能够生成一个静态站点,但我不能让jquery脚本与React一起工作。 I have a few Jquery files from an old site that I'll be transferring to the new React site. 我有一些来自旧网站的Jquery文件,我将转移到新的React网站。 Do I need to take a different approach? 我需要采取不同的方法吗?

$("#menu-toggle").click(function(e) {
        e.preventDefault();
        $("#wrapper").toggleClass("toggled");
    });
     $("#menu-toggle-2").click(function(e) {
        e.preventDefault();
        $("#wrapper").toggleClass("toggled-2");
        $('#menu ul').hide();
    });

     function initMenu() {
      $('#menu ul').hide();
      $('#menu ul').children('.current').parent().show();
      //$('#menu ul:first').show();
      $('#menu li a').click(
        function() {
          var checkElement = $(this).next();
          if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            return false;
            }
          if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            $('#menu ul:visible').slideUp('normal');
            checkElement.slideDown('normal');
            return false;
            }
          }
        );
      }
    $(document).ready(function() {initMenu();});

Ideally you shouldn't use jQuery with React unless you have a good reason (see this thread ). 理想情况下,除非有充分的理由,否则不应将jQuery与React一起使用(请参阅此主题 )。 React works by keeping the real DOM synchronised with its own virtual DOM, so it doesn't like it if jQuery tries to manipulate the DOM while React is trying to do its thing. React通过使真正的DOM与其自己的虚拟DOM保持同步来工作,因此如果jQuery在React尝试执行其操作时尝试操作DOM,它就不喜欢它。

In your case, you would probably be better off using a React component such as react-burger-menu , or impromptu-react-sidemenu . 在您的情况下,您可能最好使用React组件,例如react-burger-menu即兴反应侧 菜单 Collections of components like Material UI also offer this functionality ( material UI's drawer component ). 像Material UI这样的组件集合也提供此功能( 材质UI的抽屉组件 )。

EDIT - Removed React Components link as it is a deadlink & spam filled. 编辑 - 删除了React Components链接,因为它是一个死链接和垃圾邮件填充。

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

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