ReactJs adding active class to button

后端 未结 2 1251
梦谈多话
梦谈多话 2020-12-18 08:39

I have five buttons, dynamically created. My target is: when any button is clicked to add active class to it, and of course if any other has that active class to remove it.

2条回答
  •  情话喂你
    2020-12-18 09:19

    You need to introduce state to your component and set it in onClick event handler. For example output of render method:

    {buttons.map(function (name, index) { return this.someFunct(name)} key={ name } />; })}

    event handler (element method):

    someFunct(name) {
        this.setState({ active: name })
    }
    

提交回复
热议问题