Navaneethan Arun
Navaneethan Arun

Reputation: 386

Alternate for toggle class in react

This is html code for which show navigation icon

   <div>
        <div id="mob-nav"  className={this.state.navbar + " right"}>
       <div onClick={this.toggle.bind(this)}>
         <div>click me</div>
       <div></div>
       <div></div>
      </div>
      <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">2</a></li>
      <li><a href="#">3</a></li>
      <li><a href="#">5</a></li>
      <li><a href="#"> 6</a></li>
      <li><a href="#">7</a></li>
      <li><a href="#">8</a></li>
      </ul>
    </div>
       <div id="left_panel">
        <div className="title">
          Naveen Tool
        </div>
        <div className="user_name">
            Name
        </div>
        <div className="submenu">
          <a href="structuring">Home</a>
          <a href="#">Log out</a>
        </div>
        </div>
    </div>

Here is the css code for toggling the nav

 #mob-nav {
    position:fixed;
    z-index:1000;
    top:0px;
    bottom:0px;
    width:270px;
    -webkit-box-shadow: inset -11px 0px 33px -10px rgba(51,51,51,1);
    -moz-box-shadow: inset -11px 0px 33px -10px rgba(51,51,51,1);
    box-shadow: inset -11px 0px 33px -10px rgba(51,51,51,1);
    background-color: rgba(0,0,0,0.8);
    transform: translate3d(-270px,0,0);
    transition: transform 300ms ease;
}
.active-mob-nav #mob-nav {
    transform: translate3d(0,0,0);
}
#mob-nav ul{
    list-style:none;
    width: 100%;
padding: 0px;
}

.table_border{
  border: 1px solid black;
}

#mob-nav ul li a{
    display:block;
    text-align:center;
    text-decoration:none;
    color:#fff;
    background-color:#2e2e2e;
    padding:25px 0px;
}
#mob-nav ul li a:hover{
    background-color:#2eb187;
}
   .mob-nav-btn {
       position:absolute;
    left: 275px;
    top: 18px;
    line-height: 10px;
    padding: 16px;
       cursor:pointer;
       border-radius: 0px 10px 10px 0px;
       -moz-border-radius: 0px 10px 10px 0px;
       -webkit-border-radius: 0px 10px 10px 0px;
   }
   .mob-nav-btn > div{
       background-color: #f7f7f7;
    width: 25px;
       margin-top: 2px;
    margin-bottom: 2px;
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
    height: 2px;
       transition: all 300ms;
   }
   .mob-nav-btn:hover > div{
       width: 30px;
       border-radius:0px;
   }

In react i am trying to set the state when clicking nav button .React state is changing but the navbar is not opening .

 toggle(){
 this.setState({
  navbar :"active-mob-nav"
 })

 }

I saw the elements in developer tools ,state is changing . Navbar is not opening Can someone explain me how to toggle class on the navbar like I do on my onClick()?

Upvotes: 0

Views: 285

Answers (2)

Andrii Starusiev
Andrii Starusiev

Reputation: 7764

Wrong nesting order.

use styles:

#mob-nav.active-mob-nav {
    transform: translate3d(0,0,0);
}

And set className to top level div:

<div id="mob-nav"  className={this.state.navbar + " right"}>

https://jsfiddle.net/69z2wepo/85223/

Upvotes: 1

Panther
Panther

Reputation: 9408

I think the problem is with your CSS and not react. You CSS (.active-mob-nav #mob-nav) says active-mob-nav has a child with id mob-nav, while mob-nav has a child .active-mob-nav. Try altering your CSS to get the expected behaviour

Upvotes: 0

Related Questions