防止 ReactJS 中的多个按钮颜色激活
Prevent multiple button color activation in ReactJS
我有两个带有 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')}
/>
</>
)
}
将状态提升到Button组件之外,在父组件中处理状态,Button组件可以使用prop来确定按钮的颜色,prop可以从包含两个按钮的父组件传递
您不需要 class 组件。只需为您的本地状态使用 useState()
钩子并制作一个功能组件。此外,为每个按钮制作不同的组件似乎是多余的。试试这个,只需粘贴您的 class 当然姓名 :)
function Buttons(){
const [selectedButton, setSelectedButton] = React.useState();
return (
<div>
<button type="button"
className={selectedButton === "refuse" ? "active-classname" : "inactive-classname"}
onClick={() => setSelectedButton("refuse")}>Refus</button>
<button type="button"
className={selectedButton === "refuse" ? "active-classname" : "inactive-classname"}
onClick={() => setSelectedButton("refuse")}>Accepte</button>
</div>
);
}
我有两个带有 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')}
/>
</>
)
}
将状态提升到Button组件之外,在父组件中处理状态,Button组件可以使用prop来确定按钮的颜色,prop可以从包含两个按钮的父组件传递
您不需要 class 组件。只需为您的本地状态使用 useState()
钩子并制作一个功能组件。此外,为每个按钮制作不同的组件似乎是多余的。试试这个,只需粘贴您的 class 当然姓名 :)
function Buttons(){
const [selectedButton, setSelectedButton] = React.useState();
return (
<div>
<button type="button"
className={selectedButton === "refuse" ? "active-classname" : "inactive-classname"}
onClick={() => setSelectedButton("refuse")}>Refus</button>
<button type="button"
className={selectedButton === "refuse" ? "active-classname" : "inactive-classname"}
onClick={() => setSelectedButton("refuse")}>Accepte</button>
</div>
);
}