【发布时间】:2020-01-17 17:45:47
【问题描述】:
我有两个带有 ReactJS 和 SemanticUI 的按钮。当每一个被点击时,背景颜色都会改变。我希望一次只激活一个按钮,这意味着如果单击红色按钮,绿色按钮将停用,反之亦然,恢复默认的白色背景颜色。现在两者都可以同时点击颜色变化。
这是我的组件:
export class BoutonRefus extends Component {
constructor(props) {
super(props);
this.state = {
button: true
};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState({
button: !this.state.button
});
}
render() {
return (
<>
<div
className={
this.state.button
? "ui button medium refus true"
: "ui button medium refus false"
}
onClick={this.handleClick}
></div>
</>
);
}
}
export class BoutonAccepte extends Component {
constructor(props) {
super(props);
this.state = {
button: true
};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState({
button: !this.state.button
});
}
render() {
return (
<>
<div
className={
this.state.button
? "ui button medium accepte true"
: "ui button medium accepte false" &&
}
onClick={this.handleClick}
></div>
</>
);
}
}
这里调用了这个组件:
boutonAccepter(t) {
return (
<>
<BoutonAccepte
className="ui button medium accepte true"
onClick={() => {this.voter(true)}}
text={t('flot.split.vote.accepter')}
/>
</>
)
}
boutonRefuser(t) {
return (
<>
<BoutonRefus
className="ui button medium refus true"
onClick={() => {
this.justifierRefus()
this.voter(false)
}}
text={t('flot.split.vote.refuser')}
/>
</>
)
}
【问题讨论】:
-
而不是使用
state公开prop用于该用途。并让父控件控制哪个是活动的。
标签: javascript reactjs semantic-ui