Home Web Front-end JS Tutorial Detailed explanation of steps to highlight selected li in react implementation

Detailed explanation of steps to highlight selected li in react implementation

May 24, 2018 pm 01:40 PM
react step Highlight

This time I will bring you a detailed explanation of the steps to highlight li in react implementation. What are the precautions for selecting li highlighting in react implementation? The following is a practical case, let’s take a look.

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.

First look at the renderings:

Detailed explanation of steps to highlight selected li in react implementationCode:

    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[i]});
        }
        return 
Copy after login
    {itemList}
    } }css part

      .category {
            padding-left: 0;
            &:after {
                content: '';
                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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps of React combining TypeScript and Mobx

Detailed explanation of the steps of using React-router v4

The above is the detailed content of Detailed explanation of steps to highlight selected li in react implementation. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

How to make Google Maps the default map in iPhone

This Apple ID is not yet in use in the iTunes Store: Fix This Apple ID is not yet in use in the iTunes Store: Fix Jun 10, 2024 pm 05:42 PM

This Apple ID is not yet in use in the iTunes Store: Fix

Steps to upgrade to the latest version of WeChat (Easily master the upgrade method to the latest version of WeChat) Steps to upgrade to the latest version of WeChat (Easily master the upgrade method to the latest version of WeChat) Jun 01, 2024 pm 10:24 PM

Steps to upgrade to the latest version of WeChat (Easily master the upgrade method to the latest version of WeChat)

Detailed explanation of the steps to obtain Win11 system administrator permissions Detailed explanation of the steps to obtain Win11 system administrator permissions Mar 08, 2024 pm 09:09 PM

Detailed explanation of the steps to obtain Win11 system administrator permissions

Safari zoom issue on iPhone: Here's the fix Safari zoom issue on iPhone: Here's the fix Apr 20, 2024 am 08:08 AM

Safari zoom issue on iPhone: Here's the fix

How to close WeChat video account? Detailed steps for closing WeChat video account How to close WeChat video account? Detailed steps for closing WeChat video account Jan 02, 2024 pm 11:57 PM

How to close WeChat video account? Detailed steps for closing WeChat video account

Shazam app not working in iPhone: Fix Shazam app not working in iPhone: Fix Jun 08, 2024 pm 12:36 PM

Shazam app not working in iPhone: Fix

iPhone screenshots not working: How to fix it iPhone screenshots not working: How to fix it May 03, 2024 pm 09:16 PM

iPhone screenshots not working: How to fix it

See all articles