4 回答

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊
問(wèn)題是你的 pop 函數(shù)是像toggle
.?它將從 true 切換到 false,從 false 切換到 true。
只需將其更改為 true 即可。
可能發(fā)生的情況是這樣的:第一次單擊打開(kāi)對(duì)話框,然后關(guān)閉對(duì)話框但不要isPopped
再次設(shè)置為 false。

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您想在單擊按鈕時(shí)打開(kāi)對(duì)話框,您可以更改 pop 函數(shù)以將狀態(tài)設(shè)置為 true,如下所示:
const?pop?=?()?=>?{ ????setPop(true); };
每次單擊按鈕時(shí),當(dāng)前代碼都會(huì)切換狀態(tài),因此第一次單擊時(shí)將呈現(xiàn) Dialog2,第二次單擊時(shí)將再次隱藏。
附:
我認(rèn)為你想要的是一種打開(kāi)對(duì)話框的方法和一種關(guān)閉對(duì)話框的方法。
所以你可以做這樣的事情:
function Challange() {
? const [isPopped, setPop] = useState(false);
? const openDialog = () => {
? ? setPop(true);
? };
??
? // renember to call this function when you want to close the dialog
? const closeDialog = () => {
? ? setPop(false);
? };
? return (
? ? <>
? ? ? {isPopped && <Dialog2 />}
? ? ? <div className="challanges">
? ? ? ? <h1 className="newchallenge">Choose New Challange</h1>
? ? ? ? <button className="challangeBtn" onClick={openDialog}>
? ? ? ? ? Eat Vegetarian (31days)
? ? ? ? </button>
? ? ? ? <button className="challangeBtn" onClick={openDialog}>
? ? ? ? ? Take the bike to work (14days)
? ? ? ? </button>
? ? ? ? <button className="challangeBtn" onClick={openDialog}>
? ? ? ? ? Recycle your plastic bottles (31days)
? ? ? ? </button>
? ? ? ? <button className="challangeBtn" onClick={openDialog}>
? ? ? ? ? Use public transport to commute (31days)
? ? ? ? </button>
? ? ? ? <button className="challangeBtn" onClick={openDialog}>
? ? ? ? ? Don't fly an airplane (365days)
? ? ? ? </button>
? ? ? </div>
? ? </>
? );
}
export default Challange;

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
在調(diào)用 setPop 的地方將 pop 函數(shù)更改為true,將確保該函數(shù)在第一次 onClick 上工作,而不是在第二次單擊時(shí)切換到您想要的內(nèi)容。
const pop = () => { setPop(true); };

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以管理每種類型挑戰(zhàn)的狀態(tài),并僅在選擇一種挑戰(zhàn)時(shí)切換彈出窗口。我認(rèn)為一次只能選擇一項(xiàng)挑戰(zhàn)。
class Challenge extends React.Component {
constructor(props) {
super(props);
this.state = {
isPopped: false,
selectedChallenge: ""
}
}
selectChallenge = (challengeName) => {
this.setState({
selectedChallenge: challengeName,
});
this.togglePopped();
}
togglePopped = () => {
this.setState({isPopped: !this.state.isPopped});
}
render() {
return <div>
{this.state.isPopped && <Dialog2 challenge={this.state.selectedChallenge} hideDialog={this.togglePopped}/>}
<div className="challanges">
<h1 className="newchallenge">Choose New Challange</h1>
<button className="challangeBtn" onClick={() => {
this.selectChallenge('Eat Vegetarian (31days ')
}}>
Eat Vegetarian (31days)
</button>
<button className="challangeBtn" onClick={() => {
this.selectChallenge('Take the bike to work (14days)')
}}>
Take the bike to work (14days)
</button>
<button className="challangeBtn" onClick={() => {
this.selectChallenge('Recycle your plastic bottles (31days)')
}}>
Recycle your plastic bottles (31days)
</button>
<button className="challangeBtn" onClick={() => {
this.selectChallenge('Use public transport to commute (31days)')
}}>
Use public transport to commute (31days)
</button>
<button className="challangeBtn" onClick={() => {
this.selectChallenge("Don't fly an airplane (365days)")
}}>
Don't fly an airplane (365days)
</button>
</div>
</div>;
}
}
class Dialog2 extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div class="dialog">
<h2>Dialog2</h2>
<div>{this.props.challenge}</div>
<button onClick={this.props.hideDialog}>Hide</button>
</div>
}
}
ReactDOM.render(<Challenge/>, document.querySelector('#root'))
.dialog {
width: 500px;
background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
這是功能組件版本
function Challenge (props) {
const [isPopped, setIsPopped] = useState(false);
const [selectedChallenge, setSelectedChallenge] = useState("");
const selectChallenge = (challengeName) => {
setSelectedChallenge(challengeName);
togglePopped();
}
const togglePopped = () => {
setIsPopped(!isPopped);
}
return <div>
{isPopped && <Dialog2 challenge={selectedChallenge} hideDialog={togglePopped}/>}
<div className="challanges">
<h1 className="newchallenge">Choose New Challange</h1>
<button className="challangeBtn" onClick={() => {
selectChallenge('Eat Vegetarian (31days ')
}}>
Eat Vegetarian (31days)
</button>
<button className="challangeBtn" onClick={() => {
selectChallenge('Take the bike to work (14days)')
}}>
Take the bike to work (14days)
</button>
<button className="challangeBtn" onClick={() => {
selectChallenge('Recycle your plastic bottles (31days)')
}}>
Recycle your plastic bottles (31days)
</button>
<button className="challangeBtn" onClick={() => {
selectChallenge('Use public transport to commute (31days)')
}}>
Use public transport to commute (31days)
</button>
<button className="challangeBtn" onClick={() => {
selectChallenge("Don't fly an airplane (365days)")
}}>
Don't fly an airplane (365days)
</button>
</div>
</div>;
}
function Dialog2() {
return <div>
<h2>Dialog2</h2>
<div>{props.challenge}</div>
<button onClick={props.hideDialog}>Hide</button>
</div>
}
添加回答
舉報(bào)