0

I am trying to add class active when item in list is clicked. When second item is clicked, remove class from first item and add it to second one.

I have this code so far:

  constructor(props) {

  super(props)
  this.state = {
    clicked: false,
    showComponent: "Box1"
  };
}

addClassName = () => {
    this.setState({
      clicked: true
    });
  } 
  <ul>
  <li className = {
    this.state.clicked ? 'active' : 'inactive'
  }
onClick = {() => {
    this.toggleDiv("monday");
    this.addClassName();
  }
} data-sched = "monday"> Monday </li>

  <li className = {
    this.state.clicked ? 'active' : 'inactive'
  }
onClick = {() => {
    this.toggleDiv("thursday");
    this.addClassName();
  }
}
data-sched = "tuesday" > Thursday </li> 
</ul>

The problem is when I click first item class active is applied on both (first and second item.)

4
  • where is this.toggleDiv() ? Commented Dec 29, 2018 at 18:35
  • It triggers function not related to question, it triggers div that supposed to be shown after click. Commented Dec 29, 2018 at 18:37
  • Basically, you state needs to hold which value has been clicked, not just that a value has been clicked. Also there is an obvious code duplication that you should solve. Commented Dec 29, 2018 at 18:49
  • @Sulthan yes I do have to fix that. :/ Commented Dec 29, 2018 at 19:27

2 Answers 2

1

When clicked in state is true the both li tag have same class as that condition will be true for both of them. Try this.

  constructor(props) {

  super(props)
  this.state = {
    clicked: false,
    active:'',
    showComponent: "Box1"
  };
}


  <ul>
    <li className = {
      this.state.active=='monday' ? 'active' : 'inactive'
     }
     onClick = {() => {
      this.toggleDiv("monday");
      this.setState({active:'monday'})
     }
    } data-sched = "monday">
   Monday 
  </li>
  <li className = {
     this.state.active:'thursday'? 'active' : 'inactive'
    }
     onClick = {() => {
      this.toggleDiv("thursday");
      this.setState({active:'thursday'})
     }
    }
    data-sched = "thursday" >
   Thursday 
  </li> 
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

@user9347049 sir you get what you ask for in the question.
0

You have to save which value has been clicked:

constructor(props) {
  super(props)

  this.state = {
    selectedDay: null,
    showComponent: "Box1"
  };
}

And then use it directly (also removed some code duplication).

selectDay = day => {
   this.toggleDiv(day);
   this.setState({ 
     selectedDay: day
   });
}

render() {
   ...

   const days = [
      { name: 'monday', label: 'Monday' },
      { name: 'thursday', label: 'Thursday' }
   ];

   return (
     <ul>
       {days.map(day => (
         <li
             key={day.name}
             className={this.state.selectedDay === day.name ? 'active' : 'inactive'}
             onClick={() => this.selectDay(day.name)}
             data-sched={day.name}
         >
           {day.label}
         </li>
       )}
     </ul>
   );
}

1 Comment

Thanks for your help it is very good solution for both class adding and removing code duplication. Thanks a lot!

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.