如何在停用ReactNative上的开关按钮时添加功能?

问题描述:

我设法增加一个功能,像这样我的开关组件上按钮:如何在停用ReactNative上的开关按钮时添加功能?

  <Switch 
       onValueChange={(value)=>this.onPressIcon(_key)} 
       style={{marginBottom: 10}} 
       value={this.state.trueSwitchIsOn} 
      /> 

它触发功能onPressIcon逐一增加1 值现在,它怎么能触发其他功能时切换按钮是停用? (这样该值将减少)

+0

你应该有一个中介的作用,引用基于开关状态的两个功能。 – Ohgodwhy

+0

谢谢@Ohgodwhy :-) – Sonia

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} 
    /> 
); 
} 
+0

这个作品完全谢谢你! – Sonia

+0

@Sonia随时接受我的回答:) –

+0

@Sonia就像点击我答案旁边的复选标记一样简单:) –