如何在停用ReactNative上的开关按钮时添加功能?
问题描述:
我设法增加一个功能,像这样我的开关组件上按钮:如何在停用ReactNative上的开关按钮时添加功能?
<Switch
onValueChange={(value)=>this.onPressIcon(_key)}
style={{marginBottom: 10}}
value={this.state.trueSwitchIsOn}
/>
它触发功能onPressIcon逐一增加1 值现在,它怎么能触发其他功能时切换按钮是停用? (这样该值将减少)
答
的value
将返回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}
/>
);
}
你应该有一个中介的作用,引用基于开关状态的两个功能。 – Ohgodwhy
谢谢@Ohgodwhy :-) – Sonia