亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

onClick() 僅適用于第二次單擊 - 反應

onClick() 僅適用于第二次單擊 - 反應

狐的傳說 2023-08-24 10:25:05
我的按鈕只有在雙擊后才起作用。我在上一篇文章中了解到,問題在于彈出的狀態由所有按鈕共享,因此如果您單擊一個按鈕,它會更改每個按鈕的狀態誰能幫我找到解決方案嗎?function Challange() {  const [isPopped, setPop] = useState(false);  const pop = () => {    setPop(!isPopped);  };  return (    <>      {isPopped && <Dialog2 />}      <div className="challanges">        <h1 className="newchallenge">Choose New Challange</h1>        <button className="challangeBtn" onClick={pop}>          Eat Vegetarian (31days)        </button>        <button className="challangeBtn" onClick={pop}>          Take the bike to work (14days)        </button>        <button className="challangeBtn" onClick={pop}>          Recycle your plastic bottles (31days)        </button>        <button className="challangeBtn" onClick={pop}>          Use public transport to commute (31days)        </button>        <button className="challangeBtn" onClick={pop}>          Don't fly an airplane (365days)        </button>      </div>    </>  );}export default Challange;
查看完整描述

4 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

問題是你的 pop 函數是像toggle.?它將從 true 切換到 false,從 false 切換到 true。

只需將其更改為 true 即可。

可能發生的情況是這樣的:第一次單擊打開對話框,然后關閉對話框但不要isPopped再次設置為 false。


查看完整回答
反對 回復 2023-08-24
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

如果您想在單擊按鈕時打開對話框,您可以更改 pop 函數以將狀態設置為 true,如下所示:

const?pop?=?()?=>?{
????setPop(true);
};

每次單擊按鈕時,當前代碼都會切換狀態,因此第一次單擊時將呈現 Dialog2,第二次單擊時將再次隱藏。

附:

我認為你想要的是一種打開對話框的方法和一種關閉對話框的方法。

所以你可以做這樣的事情:

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;


查看完整回答
反對 回復 2023-08-24
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

在調用 setPop 的地方將 pop 函數更改為true,將確保該函數在第一次 onClick 上工作,而不是在第二次單擊時切換到您想要的內容。

const pop = () => {
    setPop(true); 
};


查看完整回答
反對 回復 2023-08-24
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

您可以管理每種類型挑戰的狀態,并僅在選擇一種挑戰時切換彈出窗口。我認為一次只能選擇一項挑戰。


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>

}



查看完整回答
反對 回復 2023-08-24
  • 4 回答
  • 0 關注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號