选中高亮的两种模式

524 阅读1分钟

选中高亮的两种模式

1.如果是纯数组的方式

class Shine extends React.Component {
   state = {
      currentIndex: 0
    }
   handleSelect=(e)=>{
    this.setState({
      currentIndex:parseInt(e.target.getAttribute('index'))
    })
  }
  render() {
    let data= ['我', '爱', '敲', '代', '码'];
    return (
	 <div>
            {
              data.map((item,index) => {
                return (
                  <div key={index} index={index}  className={this.state.currentIndex === index ?'active' : ''} onClick={this.handleSelect.bind(this)}>
                    {item}
                  </div>
                )
              })
            }
          </div>

    )
  }
}

2.如果是数组对象的形式,可以设置一个开关,当选中的时候把开关打开,设置高亮