0

I am implementing a menu toggling in react js . the problem is when i click on one li item .the last li item is always opening no matter what. i only want that particular menu to be shown. other menus should be hidden. i can't find the problem.

code pen link : https://codepen.io/nahidmbstu/pen/QQNdjN

code is here

var el = document.querySelector("#app")

class Editor extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.state = { display : false,  
                  display1 : false , 
                  display2 : false, 
                  display3 : false,  };
  }

  handleChange(e) {
    var ID = e.target.id

    console.log(ID )
    if (ID == 1 )
    {
      this.setState({ display: ! this.state.display } );
    }
    else if (ID == 2)
    {
      this.setState({ display1: ! this.state.display1 } );

    }
      else if (ID == 3)
    {
      this.setState({ display2: ! this.state.display2 } );

    }
      else (ID == 4)
    {
       this.setState({ display3: ! this.state.display3 } );

    }

  }


  render() {
    return (
      <div className="container">
        <div className= "row">
          <div className= "col-md-12 offset-3">
            Header
          </div>
        </div>
        <div className= "row">

          <div className= "col-md-4 offset-3">

                   <ul class = "menu-list">
                     <li class = "primary-menu" id = "1" onClick = { this.handleChange }>fastfood &#8595;
                        <ul class = "submenu-list">
                             <li class = "submenu" style={ { display: this.state.display ? 'block' : 'none' }}>Berger</li>
                             <li class = "submenu" style={ { display: this.state.display ? 'block' : 'none' }}>Pizza</li>
                        </ul>
                    </li>

                    <li class = "primary-menu">Coffee</li>

                    <li class = "primary-menu"  id = "2" onClick = { this.handleChange }>Tea &#8595;
                       <ul class = "submenu-list" >
                          <li class = "submenu" style={ { display: this.state.display1 ? 'block' : 'none' }}>Black tea</li>
                           <li class = "submenu" style={ { display: this.state.display1 ? 'block' : 'none' }}>Green tea</li>
                      </ul>
                    </li>

                    <li class = "primary-menu"  id = "3" onClick = { this.handleChange }>Milk &#8595;
                        <ul class = "submenu-list">
                           <li class = "submenu" style={ { display: this.state.display2 ? 'block' : 'none' }}>cow milk</li>
                             <li class = "submenu" style={ { display: this.state.display2 ? 'block' : 'none' }}>goat milk</li>
                        </ul>
                     </li>
                      <li class = "primary-menu"  id = "4" onClick = { this.handleChange }>Cold Drinks  &#8595;
                        <ul class = "submenu-list">
                           <li class = "submenu" style={ { display: this.state.display3 ? 'block' : 'none' }}>Coke</li>
                           <li class = "submenu" style={ { display: this.state.display3 ? 'block' : 'none' }}>Sprite</li>
                        </ul>
                     </li>
                  </ul>

          </div>
           <div className= "col-md-8 offset-3">

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

ReactDOM.render(<Editor />, el);

2 Answers 2

1

You forgot a if :

 else (ID == 4){
 =>
 else if (ID == 4){
Sign up to request clarification or add additional context in comments.

2 Comments

You cannot have a predicate with an else statement. An else statement will be called if none of the if above has been triggered. In your code, the last block is not in the if statement and is call every time
If this solvers your problem, can you please accept the answer (white check mark below the up/down-vote buttons) to indicate this question is answered. Thanks.
1

your last if clause is not working as you want it to.

https://codepen.io/anon/pen/gvMwXL

updated your codepen

//....
else if (ID == 4) //add "if" here
{
   this.setState({ display3: ! this.state.display3 } );

}
//....

1 Comment

it does but the (ID == 4) does not due to it will be evaluated as a usual expression but not as an if check - so it will always be executed if the if checks above failed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.