React onClick add class to clicked element but remove from the others

后端 未结 2 1724
一向
一向 2021-01-26 11:57

so what i try to achieve here is very similar to what is done here Transition flex-grow of items in a flexbox

But what i wonder how this could be done with React say i h

2条回答
  •  既然无缘
    2021-01-26 12:34

    You can set active index on click:

    // State
    this.state = {
      activeIndex: null
    };
    // event
    onElementClicked(e) {
      this.setState({ activeIndex: e.target.index })
    }
    // class to use
    className={this.index === this.state.activeIndex ? 'big-size' : ''}
    

提交回复
热议问题