[英]How to detect checkbox checked or not using react native?
在我的場景中,我正在嘗試為checkbox
check 或uncheck
編寫邏輯。 如果檢查意味着需要打印一些字符串或取消選中意味着需要打印其他東西。 如何使用本機反應來實現這一點?
我的代碼如下
checked={this.state.checked}
onPress={() => {
this.actiondone();
}}
actiondone(){
//Here how to detect checked or not with if else
}
您可以使用您的 state 來實現此目的。 如果您按下復選框時當前選中了 state ( this.state.checked = true
),則未選中預期的 state,反之亦然。 然后您可以將您的 state 設置為與原來相反。
所以基本上你對復選框的當前 state 執行相反的操作,然后將 state 設置為所需的 state (異步)。
如下所示:
<CheckBox
checked={this.state.checked}
onPress={() => this.actionDone()}
/>
actionDone() {
if (this.state.checked) {
// Do what you want when the checkbox is unchecked
} else {
// Do what you want when the checkbox is checked
}
// Set the checkbox to the desired state
this.setState({checked: !this.state.checked})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.