Home > Web Front-end > JS Tutorial > body text

React implements the method of highlighting the selected li when clicking on it

不言
Release: 2018-07-04 09:57:43
Original
3254 people have browsed it

This article mainly introduces the example code of react to highlight the selected li. There are many li on the page. You need to highlight the one you click on. The content is quite good. I would like to share it with you now and give it as a reference.

Although it is just a simple function, it is better to record it. There are many li's on the page, and the one you click on will be highlighted. When I used jq back then, it was quite simple. I just added the selected element to addClass, then removed its sibling element, and then wrote an active style. Now to use react to implement similar operations, what I think of is to use a currentIndex and switch by judging which element the currentIndex is on.

Let’s take a look at the renderings first:


Code:

  class Category extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      currentIndex: 0
    }
    this.setCurrentIndex = this.setCurrentIndex.bind(this)
  }
  setCurrentIndex(event) {
    this.setState({
      currentIndex: parseInt(event.currentTarget.getAttribute('index'), 10)
    })
  }
  render() {
    let categoryArr = ['产品调整', '接口流量', '负载均衡', '第三方软件调整',
              '安全加固', '性能控制', '日志查询', '业务分析'];
    let itemList = [];
    for(let i = 0; i < categoryArr.length; i++) {
      itemList.push(<li key={i}
               className={this.state.currentIndex === i ? &#39;active&#39; : &#39;&#39;}
               index={i} onClick={this.setCurrentIndex}
             >{categoryArr[i]}</li>);
    }
    return <ul className="category">{itemList}</ul>
  }
}
Copy after login

css part

   .category {
      padding-left: 0;
      &:after {
        content: &#39;&#39;;
        display: block;
        clear: both;
      }
      li {
        float: left;
        width: 23%;
        height: 40px;
        margin-right: 10px;
        margin-bottom: 10px;
        border: 1px solid $border-color;
        list-style: none;
        color: $font-color;
        line-height: 40px;
        text-align: center;
        font-size: 14px;
        cursor: pointer;
        &.active {
          border-color: #079ACD;
        }
     }
Copy after login

Isn’t it very simple. It is to add an index flag to the element when generating these li, and then when clicked, use event.currentTarget.getAttribute('index') to take out the index, then set the value of currentIndex, and then write css The active style is done.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to Flexbox layout using React Native

Implementation method of SSR based on React and Redux

The above is the detailed content of React implements the method of highlighting the selected li when clicking on it. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template