react,怎么实现列表点击那一项样式改变,再次点击样式变成原先的样式,点击别的项,样式变到这一项上

就是下面这个,最开始点击1,然后在点击2,然后在点击2

https://img.mukewang.com/5c4d4ae300012bfd01460098.jpg

https://img3.mukewang.com/5c4d4ae60001472401600112.jpg

https://img1.mukewang.com/5c4d4ae8000176c301570122.jpg

说一下大概具体思路就行,当然有代码更好,这个列表是遍历出来的- -

慕桂英546537
浏览 2564回答 1
1回答

慕少森

import classNames from 'classnames';constructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.state = {&nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; index: 0,&nbsp; &nbsp; };}handleClick (index) {&nbsp; // toggle效果你可以先判定是否等同,然后设置为-1&nbsp; this.setState({index});}render() {&nbsp; some.map((item ,idx) => {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <li className={ classNames({active: idx === this.state.index}) } onClick={ this.handleClick.bind(this, idx) }></li>&nbsp; &nbsp; )&nbsp; })}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript