How to remove other className? when implementing something like highlighting in react

problem description

implement a highlight function in the react project. When you click on the DOM, how to remove the className? of the sibling elements

the environmental background of the problems and what methods you have tried

[environment]: the array loop is looped in the parent, and the contents of the loop are provided in the child component. (for example, the child component

  • is looped out in the parent component, but the contents of
  • are written in the child component)

    [tried method]:
    1, use the highlight method;
    2, since the highlight is not allowed in the child component, then do it in the parent component in the same way, but the click event is mounted on the call to the child component.

    related codes

    / / Please paste the code text below (do not replace the code with pictures)
    method 1:

    
    constructor () {
        super();
        this.state = {
          active : 0
        }
      }
    handleClick = (event)=>{
        this.setState({active: parseInt(event.target.getAttribute("index"), 10)})
    }
    render() {...
        return (<div onClick={this.handleClick}>...</div>)
    }

    method 2:

    render (){
        let items = list.map(()=>{
            return <TargetSource onClick={this.handleClick} />
        })
        return (
            ...
            xx ? xxx: {items}
        )
    }
    

    what result do you expect? What is the error message actually seen?

    [expected result]: similar to highlight

    [errors actually seen]:
    method 1: the highlight effect is added to the current element when you click, but the highlight effect of other sibling elements is not removed
    method 2: click without any reaction

    [later]: I started to do the React project without learning React, so I can only learn it by myself, so I"m a rookie. Please advise me if there"s something wrong, don"t spray it if you don"t like it, thank you.

  • Mar.29,2021

    take the sequence with you when traversing, and compare it with state.
    refer to the following code

    state = {
        index:0
    }
    handleClick = (index) => {
        this.setState({index})
    }
    render (){
        const {index} = this.state;   
        return (
            list.map((el,i)=>{
                return <TargetSource className={index===i?"current":""} onClick={(i)=>this.handleClick(i)} />
            })
        )
    }
    MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
    MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3aadf-2ba7d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
    MySQL Errno : 1021
    Message : Disk full (/tmp/#sql-temptable-64f5-1b3aadf-2ba7d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
    Need Help?