简体   繁体   中英

Mobile menu toggle button moves the page to the top in React

I've created this NavBar component with the mobile version.

But when I click on menu/close buttons on the phone the toggle works, but moves the page to the top. Does someone have an idea of how I can prevent that?

var MobileMenu = React.createClass({
      getDefaultProps: function(){
        return {
            isOpen: false
        };
      },
      openForm: function(){
            window.open('http://localhost:8080.com/form', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes')
        },
      render: function(){
        if (this.props.isOpen){
        return (
            <div className="navMenuButtons">
              <NavLink to="/" className="navMenuButton">HOME</NavLink>
              <NavLink to="/about" className="navMenuButton">ABOUT</NavLink>
              <NavLink to="/faq" className="navMenuButton">FAQ</NavLink>
              <div className="margin-auto">
                <NavLink className="navButton applyButton" onClick={this.openForm}>
                 APPLY
                </NavLink>
              </div>
            </div>
        )
      }
        return null;
      }
    });
    var NavBar = React.createClass({
      getInitialState: function()
            {
                return {
                    isOpen: false
                };
            },
          toggleMenu: function(){
          this.setState({isOpen: !this.state.isOpen});
        },
          openForm: function(){
                window.open('http://localhost:8080/form', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes')
            },
      render: function(){
          var mobileMenuClass = this.state.isOpen ? 'navBar navBarOpen cl-effect-21' : 'navBarClose navBar cl-effect-21';
          var mobileButton = this.state.isOpen ? 'CLOSE' : 'MENU';
          return (
            <div>
                <div className={mobileMenuClass}>
                <a href="#" className="mobile_toggle">
                  <div className="toggle_labels">
                    <div onClick={this.toggleMenu}className="menu navButton">{mobileButton}</div>
                  </div>
                </a>
              <div className="navMenuButtons_desk">
                <NavLink to="/" className="navMenuButton">HOME</NavLink>
                <NavLink to="/about" className="navMenuButton">ABOUT</NavLink>
                <NavLink to="/faq" className="navMenuButton">FAQ</NavLink>
                <div className="button_right">
                  <NavLink target="_blank" className="navButton applyButton" ><div onClick={this.openForm}>
                    APPLY</div>
                  </NavLink>
                </div>
              </div>
              <MobileMenu isOpen={this.state.isOpen} onToggle={this.toggleMenu} />
              </div>
            </div>
          );
        }
    });
    var App = React.createClass({
      render: function(){
        console.log(this.props.location.pathname);
        const location = this.props.location;
        return (
          <div>
            <NavBar />
            { this.props.children }
            <Footer />
          </div>
        );
      }
    });

This is probably the problem with your anchor tag instead of React. Try setting javascript:void(0) on your href , or removing the #.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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