且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ReactJs向按钮添加活动类

更新时间:2023-12-06 13:29:52

您需要将状态引入组件并进行设置在 onClick 事件处理程序中。例如渲染方法的输出:

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

<div>
    {buttons.map(function (name, index) {
        return <input
                 type="button"
                 className={this.state.active === name ? 'active' : ''}
                 value={name}
                 onClick={() => this.someFunct(name)}
                 key={ name } />;
   })}
</div>

事件处理程序(元素方法):

event handler (element method):

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