5

enter image description here

For example, just switching the active classes btw to li. But not with router. I have list like this it is not cicle with map() method. It is static list.

 constructor(props) {
        super(props);
        const parsedUrlQuery = window.location.pathname;
        this.state = {
            isActive: true
        }
    }

  <ul className='bread list-inline'>
       <li className={this.state.isActive ? 'navigation--active' : ''}>All projects</li>
       <li> My projects</li>
  </ul>

the problem is i don not know how to remove and add class for static list. Could u help me with that.

2
  • 1
    adding/removing class on the basis of what? which action? Commented Jan 19, 2018 at 7:35
  • @MayankShukla I need to switch active className btw to li, but without routers. Is it possible to do that? Commented Jan 19, 2018 at 7:50

2 Answers 2

6

You have to change state somehow. Button click, input change or even from a child component handler of some sort.

In this example, I'm using onClick in each li element that triggers such state change:

class App extends React.Component {
  constructor() {
    super()

    this.state = {
      activeItem: -1,
      items: ['All Projects', 'My Projects'],
    }
  }

  handleItemClick(index) {
    this.setState({
      activeItem: index,
    })
  }

  render() {
    return (
      <div>
        <ul className='bread list-inline'>
          {this.state.items.map((item, index) =>
            <li
              key={index}
              className={this.state.activeItem === index ? 'navigation--active' : ''}
              onClick={this.handleItemClick.bind(this, index)}
            >
              {item}
            </li>
          )}
        </ul>
      </div>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
)
ul > li {
  cursor: pointer;
}

li.navigation--active {
  text-decoration: underline;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

this method just adds the active class to first <li>, but I need to add active class when i click on of the li
@Feruza I haven't got that, sorry. I've just updated my answer. Hope it helps now :)
1

Let say your li is below

<li className={this.state.isActive ? 'navigation--active' : ''}>All projects</li>

Now change you li to this below LI and add OnClick function "liCkickFun"

    <li className='navigation--active' onClick={this.liCkickFun}>All projects</li>

Now in your component add below function

liCkickFun = (event) => {
  //check if clicked element has active class then remove it 
  //and if don't has class then add it
     if(event.target.classList.contains('navigation--active')){
       event.target.classList.remove('navigation--active');
     } else { 
       event.target.classList.add('navigation--active');         
     }
}

Hope this will help :)

Comments

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.