1 回答

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
所以我為你嘗試了這個(gè):
這樣做是定義了 3 個(gè)用于顯示鏈接或隱藏的處理程序,基本上是在單擊按鈕時(shí)切換顯示/隱藏。
class Menu extends React.Component {
constructor(props) {
super(props);
this.state = {
value1: "Link 1",
showValue1: false,
value2: "Link 2",
showValue2: false,
value3: "Link 3",
showValue3: false
};
}
click1 = () => {
this.setState({ showValue1: !this.state.showValue1 });
};
click2 = () => {
this.setState({ showValue2: !this.state.showValue2 });
};
click3 = () => {
this.setState({ showValue3: !this.state.showValue3 });
};
render() {
return (
<div>
<button onClick={() => this.click1()}>Link 1</button>
{this.state.showValue1 && <h2>{this.state.value1}</h2>}
<br />
<button onClick={() => this.click2()}>Link 2</button>
{this.state.showValue2 && <h2>{this.state.value2}</h2>}
<br />
<button onClick={() => this.click3()}>Link 3</button>
{this.state.showValue3 && <h2>{this.state.value3}</h2>}
<br />
</div>
);
}
}
添加回答
舉報(bào)