onClick
事件触发函数actionClick
, 是不带参数的,onClick= { actionItem }
onClick = { this.activateButton.bind(this, 0) }
下面是一个向组件内函数传递参数的小例子
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.scss'
class TabButton extends React.Component {
constructor(props) {
super(props);
this.state = {
markArray: [0, 0, 0, 0],
itemClassName:'tab-button-item'
};
this.activateButton = this.activateButton.bind(this);
}
// 根据参数id, 来确定激活四个item中的哪一个
activateButton(id) {
let tmpMarkArray = [0, 0, 0, 0]
tmpMarkArray[id] = 1;
this.setState({markArray: tmpMarkArray});
}
render() {
return (
<div className = "tab-button" >
<div className = {(this.state.markArray)[0] ? "tab-button-item-active" : "tab-button-item" } onClick = { this.activateButton.bind(this, 0) } > 零 </div>
<div className = {(this.state.markArray)[1] ? "tab-button-item-active" : "tab-button-item" } onClick = { this.activateButton.bind(this, 1) } > 壹 </div>
<div className = {(this.state.markArray)[2] ? "tab-button-item-active" : "tab-button-item" } onClick = { this.activateButton.bind(this, 2) } > 贰 </div>
<div className = {(this.state.markArray)[3] ? "tab-button-item-active" : "tab-button-item" } onClick = { this.activateButton.bind(this, 3) } > 叁 </div>
</div>)
}
}
ReactDOM.render( < TabButton / > , document.getElementById("root"));
上面的例子也可以通过event.target.value
快速实现,但这个demo的扩展性更好, 在版本迭代过程中, 我们可以传递数量更多的参数, 详尽的描述UI层当前的状态, 方便业务的扩展
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有