如何在 React Native 上停用 Switch Button 时添加功能?
How to add a function when deactivating Switch Button on ReactNative?
我设法为我的组件上的“切换”按钮添加了一个功能,如下所示:
<Switch
onValueChange={(value)=>this.onPressIcon(_key)}
style={{marginBottom: 10}}
value={this.state.trueSwitchIsOn}
/>
它触发 onPressIcon 函数,该函数将值递增 1。
现在,当 Switch 按钮被停用时,它如何触发另一个功能? (所以价值会减少)
当开关处于活动状态时,value
将 return true
,否则 false
。因此,您可以使用它来根据值触发两个不同的函数。所以像下面这样:
onPressIcon = (value) => {
// if the switch is activated, execute increment function
if (value) {
this.incrementSomething();
// ... rest of code
} else {
// switch is deactivated, execute other function
this.otherFunction();
// ... rest of code
}
}
// render
render () {
return(
//... rest of code
<Switch
onValueChange={(value) => this.onPressIcon(value)}
style={{marginBottom: 10}}
value={this.state.trueSwitchIsOn}
/>
);
}
我设法为我的组件上的“切换”按钮添加了一个功能,如下所示:
<Switch
onValueChange={(value)=>this.onPressIcon(_key)}
style={{marginBottom: 10}}
value={this.state.trueSwitchIsOn}
/>
它触发 onPressIcon 函数,该函数将值递增 1。 现在,当 Switch 按钮被停用时,它如何触发另一个功能? (所以价值会减少)
当开关处于活动状态时,value
将 return true
,否则 false
。因此,您可以使用它来根据值触发两个不同的函数。所以像下面这样:
onPressIcon = (value) => {
// if the switch is activated, execute increment function
if (value) {
this.incrementSomething();
// ... rest of code
} else {
// switch is deactivated, execute other function
this.otherFunction();
// ... rest of code
}
}
// render
render () {
return(
//... rest of code
<Switch
onValueChange={(value) => this.onPressIcon(value)}
style={{marginBottom: 10}}
value={this.state.trueSwitchIsOn}
/>
);
}