如何防止在React Native的Switch组件上多次点击。

huangapple go评论75阅读模式
英文:

How to prevent multiple click on Switch component from react native

问题

我在React Native的Switch组件中遇到了问题,问题是在API完成之前,用户能够多次点击Switch组件。那么,如何防止用户多次点击Switch组件?

我使用的是类组件。

              <Switch
                trackColor={{ false: MONOCHROME_5, true: POLYCHROME_1 }}
                ios_backgroundColor={MONOCHROME_5}
                onValueChange={() => {
                  // 进行某些操作
                }}
                onChange={(e) => console.log(e)}
                value={this.state.enabled}
              />

已经尝试使用状态(state)来使用onTouchStart属性禁用组件,但仍然能够多次点击。

英文:

I have an issue with Switch component from react native, the issue is user able to multiple click on switch component before API completed. So, how to prevent user to multiple click on switch component?

I'm using class component btw.

              &lt;Switch
                trackColor={{ false: MONOCHROME_5, true: POLYCHROME_1 }}
                ios_backgroundColor={MONOCHROME_5}
                onValueChange={() =&gt; {
                  //do something
                }}
                onChange={(e) =&gt; console.log(e)}
                value={this.state.enabled}
              /&gt;

Already try to use state to disable the component with onTouchStart props, but it was still able to multiple click

答案1

得分: 2

使用 useState 钩子来维护一个布尔值的状态,该状态将设置开关的 disabled 属性。然后,您可以在 onChangeonValueChange 函数中切换此布尔值以禁用开关,然后在 API 调用完成后重新启用。

const [switchDisabled, setSwitchDisabled] = useState(false)

<Switch
    trackColor={{ false: MONOCHROME_5, true: POLYCHROME_1 }}
    ios_backgroundColor={MONOCHROME_5}
    onValueChange={() => {
        setSwitchDisabled(true)
        // 等待 API 调用
        setSwitchDisabled(false)
    }}
    onChange={(e) => console.log(e)}
    value={this.state.enabled}
    disabled={switchDisabled}
/>
英文:

Use a useState hook to hold the state of a boolean value which will set the switch's disabled prop. You can then toggle this boolean in your onChange or onValueChange function to disable the switch, and then re-enable once the API call has completed.

const [switchDisabled, setSwitchDisabled] = useState(false)

&lt;Switch
    trackColor={{ false: MONOCHROME_5, true: POLYCHROME_1 }}
    ios_backgroundColor={MONOCHROME_5}
    onValueChange={() =&gt; {
        setSwitchDisabled(true)
        //await API Call
        setSwitchDisabled(false)
    }}
    onChange={(e) =&gt; console.log(e)}
    value={this.state.enabled}
    disabled={switchDisabled}
/&gt;

答案2

得分: 0

我从我的合作伙伴那里得到了答案

这是方法

              <Switch
                disabled={this.state.isDisableToggle}
                onResponderStart={() => {
                  if (Platform.OS === 'ios') { // 处理防止iOS激活时的双击
                    this.setState({ isDisableToggle: true })
                  }
                }}
                onChange={() => {
                  if (Platform.OS === 'android') { // 处理防止Android激活时的双击
                    this.setState({ isDisableToggle: true })
                  }
                }}
                onValueChange={() => {
                  setTimeout(() => { // 处理防止点击切换被禁用
                    this.setState({ isDisableToggle: false });
                  }, 2000);
                  // 做一些事情
                }}
                value={this.state.isEnabledSwitch}
              />

这与@Stitt的概念几乎相同,但我们发现了如何防止iOS和Android设备的双击。

英文:

I got the answer from my partner

here is the way

              &lt;Switch
                disabled={this.state.isDisableToggle}
                onResponderStart={() =&gt; {
                  if (Platform.OS === &#39;ios&#39;) { // handle prevent click toggle for ios activated
                    this.setState({ isDisableToggle: true })
                  }
                }}
                onChange={() =&gt; {
                  if (Platform.OS === &#39;android&#39;) { // handle prevent click toggle android activated
                    this.setState({ isDisableToggle: true })
                  }
                }}
                onValueChange={() =&gt; {
                  setTimeout(() =&gt; { // handle prevent click toggle disabled
                    this.setState({ isDisableToggle: false });
                  }, 2000);
                  // do something
                }}
                value={this.state.isEnabledSwitch}
              /&gt;

it was almost same with concept from @Stitt, but we found out how to prevent double click from iOS and android device

huangapple
  • 本文由 发表于 2023年6月19日 18:36:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定