简体   繁体   中英

React JS onClick Open Current Dropdown Menu

I am having n number of dropdown menus in React JS. When I am clicking nth menu item, I want to open the corresponding dropdown menu. But what I am getting now is, on clicking a menu item, all the dropdowns are getting opened. How this can be achieved?

class Menubar extends React.Component {
  constructor() {
    super();
    this.state = {
      clicked: false
    };
    this.handleClick = this.handleClick.bind(this);
    this.handleOutsideClick = this.handleOutsideClick.bind(this);
  }

  componentWillMount() {
        document.addEventListener('click', this.handleOutsideClick, false);
    }

    componentWillUnmount(){
        document.removeEventListener('click', this.handleOutsideClick, false);
    }
  handleClick() {
    this.setState({clicked: !this.state.clicked});
  }
  handleOutsideClick(event){
        if (!this.refs.megaMenu.contains(event.target)) {
      this.setState({
                clicked: false
            });
        } 
  }
  render() {
    return (
      <div className="container">
        <div className="menu-bar">

          <div className="menu-bar-item" ref="megaMenu">
            <a className="menu-bar-link" href="#" onClick={this.handleClick}>First Menu</a>
            <div className={"mega-menu"+" "+this.state.clicked}>
              <div className="mega-menu-content">
                <p>First Menu</p>
              </div>
            </div>
          </div>

          <div className="menu-bar-item" ref="megaMenu">
            <a className="menu-bar-link" href="#" onClick={this.handleClick}>Second Menu</a>
            <div className={"mega-menu"+" "+this.state.clicked}>
              <div className="mega-menu-content">
                <p>Second Menu</p>
              </div>
            </div>
          </div>

          <div className="menu-bar-item" ref="megaMenu">
            <a className="menu-bar-link" href="#" onClick={this.handleClick}>Third Menu</a>
            <div className={"mega-menu"+" "+this.state.clicked}>
              <div className="mega-menu-content">
                <p>Third Menu</p>
              </div>
            </div>
          </div>

          <div className="menu-bar-item" ref="megaMenu">
            <a className="menu-bar-link" href="#" onClick={this.handleClick}>Fourth Menu</a>
            <div className={"mega-menu"+" "+this.state.clicked}>
              <div className="mega-menu-content">
                <p>Fourth Menu</p>
              </div>
            </div>
          </div>

        </div>
      </div>
    );
  }
}

ReactDOM.render(
  <Menubar />,
  document.getElementById('example')
);

Codepen Demo Here

You have used a single state ie clicked for all of the menu items, this will triger for all the menus when there is a setState called.

you should have seperate state for checking the clicks for each menu items. or make an array say clicked[] . and then change the value of a particular clicked state. eg: ...

<div className="menu-bar-item" ref="megaMenu">
            <a className="menu-bar-link" href="#" onClick={this.handleClick.bind(this,1)}>Second Menu</a>
            <div className={"mega-menu"+" "+this.state.clicked[1]}>
              <div className="mega-menu-content">
                <p>Second Menu</p>
              </div>
            </div>
          </div>

.... and define the handleClick as

 handleClick(index,e) {
  let clicked=this.state.clicked;
    clicked[index]=!clicked[index]
    this.setState({clicked:clicked});
  }

here is the codepen link : http://codepen.io/anon/pen/oLRbmq

由于此<div className={"mega-menu"+" "+this.state.clicked}> ,所有下拉列表都被选中,它们都使用来自状态的相同变量 - clicked ,因此如果您单击一个全部它们会改变状态。如果您只想要选择特定的下拉列表,则需要在状态中为每个相应的下拉元素声明一个新变量,并相应地更改逻辑handleClick

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