忽然笑
2021-12-02 19:50:03
我制作了兩個按鈕組件,當我單擊一個組件時,我想渲染另一個 onClick 以便能夠禁用(因為它們看起來不一樣)。 const match = id; const checkId = this.state.pokemon.includes(match); {!checkId || this.state.isDisabled === true ? ( <button onClick={() => this.setState({ isDisabled: true }) } > Get Pokemon </button> ) : ( <Button disabled/> )} </div>問題是按鈕禁用僅在我刷新時呈現,因為滿足檢查 ID 的條件但我在單擊后直接切換到禁用按鈕時遇到問題
2 回答

人到中年有點甜
TA貢獻1895條經驗 獲得超7個贊
替換為
<button disabled={!checkId || this.state.isDisabled}
onClick={() =>
this.setState({
isDisabled: true
})
}
/>
Get Pokemon
</button>

三國紛爭
TA貢獻1804條經驗 獲得超7個贊
以下條件似乎有問題:
{!checkId || this.state.isDisabled === true ? (
在我看來,條件應該改為:
!checkId || !this.state.isDisabled
,因為據我所知,每當單擊按鈕時,isDisabled
狀態都會設置為true
將條件評估為真,因此,永遠不會執行假情況。
如果您需要執行一次, per match
,您甚至可以將條件更改為:
(!checkId && !this.state.isDisabled)
希望這可以幫助
添加回答
舉報
0/150
提交
取消