JavaScript change state on single li react

How can i add class to single li element using onClick? At the moment when i click on whatever li, all div's are getting item-active class when state is changed.

I do have index from mapped array, but i'm not sure where (i believe in handleClick()?) should i use it to make it working...

//import {cost} from '...';
export class CostFilter extends Component {
  constructor(props) {
    super(props);

    this.state = {active: "item-not-active"};
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick(){
    let isActive = this.state.active === "item-not-active" ? "item-active" : "item-not-active";
    this.setState({active: isActive});
  }


  render() {
    return (
        <ul>
          {cost.map((element, index) =>
              <li onClick={this.handleClick}  value={`cost-${element.cost}`} key={index}>
                  <div className={`hs icon-${element.cost} ${this.state.active}`}></div>
              </li>
          )}
        </ul>
    );
  }
}
Answer:1

Try something like this:

export class CostFilter extends Component {
  constructor(props) {
    super(props);
    this.state = {activeIndex: null};
  }
  handleClick(index) {
    let activeIndex = this.state.activeIndex === index ? null : index;
    this.setState({activeIndex});
  }

  render() {
    return (
      <ul>
        {cost.map((element, index) =>
          <li onClick={this.handleClick.bind(this, index)}  value={`cost-${element.cost}`} key={index}>
            <div className={`
                   hs 
                   icon-${element.cost} 
                   ${this.state.activeIndex === index && 'item-active'}
                 `}></div>
          </li>
        )}
      </ul>
    );
  }
}
Answer:2

You can store index of the clicked element in the state of your component and then check in your render function in map if the current index is equal to the key in the state. Something like this :

let data = ['Item 1', 'Item 2', "Item 3"];

class Test extends React.Component {
    constructor(){
        this.state = {
           active: null
        }
    }

    handleClick(i){ 
       this.setState({active: i});
    }

    render(){
       return <ul>{this.props.data.map((item, i) => <li key={i} className={this.state.active === i ? 'active' : ''} onClick={this.handleClick.bind(this, i)}>{item}</li>)}</ul>
    }
}

React.render(<Test data={data}/>, document.getElementById('container'));

Here is a fiddle.

Answer:3

I hope this is what are you looking for. fiddle

const data = ['Hello', 'World']

class Example extends React.Component {
  constructor(){
    this.state = {
        isActive: -1
    }
  }
    click(key,e){
    this.setState({
        isActive: key
    })
  }
    render(){
    const costsList = this.props.costs.map((item, key) => {
        return <li key={key} 
            className={this.state.isActive === key ? 'foo' : ''}
            onClick={this.click.bind(this, key)}>
            {item}
       </li>
    })
    return <ul>
        {costsList}
    </ul>
  }
}

React.render(<Example costs={data}/>, document.getElementById('container'));
Answer:4

I have multiple pages in my Angular app that the user can change quite a bit, i.e. open tabs and pills, modals, etc. Let's say, for example, they open a modal and then click a link in that modal which ...

I have multiple pages in my Angular app that the user can change quite a bit, i.e. open tabs and pills, modals, etc. Let's say, for example, they open a modal and then click a link in that modal which ...

I am generating random numbers and appending with the previous variable and trying to get the value of images. but also the random number only generated. How can I do this ? Here's my code var img1 =...

I am generating random numbers and appending with the previous variable and trying to get the value of images. but also the random number only generated. How can I do this ? Here's my code var img1 =...

  1. generate random image
  2. generate random image python
  3. generate random image javascript
  4. generate random image url
  5. generate random image name php
  6. generate random image matlab
  7. generate random image java
  8. generate random image php
  9. generate random image using base64 string
  10. generate random image online
  11. generate random image numpy
  12. generate random image c#
  13. generate random image linux
  14. generate random image node
  15. opencv generate random image
  16. imagemagick generate random image
  17. generate random background image
  18. opencv generate random image python
  19. swift generate random image
  20. js generate random image

I continuously run into this issue when designing sites that have scroll-based events (animation, parallax, etc etc etc). Is there any reliable to test if a browser halts repainting on scroll, so I ...

I continuously run into this issue when designing sites that have scroll-based events (animation, parallax, etc etc etc). Is there any reliable to test if a browser halts repainting on scroll, so I ...

<ul> <a href="#Project"> <li onclick="project()">Projects</li> </a> </ul> a tab appears and you can click on it, which runs a javascript function ...

<ul> <a href="#Project"> <li onclick="project()">Projects</li> </a> </ul> a tab appears and you can click on it, which runs a javascript function ...