简体   繁体   English

SegmentedControlIOS forceUpdate

[英]SegmentedControlIOS forceUpdate

I react-native's SegmentedControlIOS component for choosing between three values which works fine so far. 我反应本机的SegmentedControlIOS组件,以选择到目前为止可以正常工作的三个值。 However, when my application is loading data I want to ensure that the user can not change the value associated with that control as this would cause concurrency issues and interrupts the update. 但是,当我的应用程序正在加载数据时,我想确保用户无法更改与该控件关联的值,因为这会导致并发问题并中断更新。

Thus I decided that the method for handling the control's value change only calls the callback supplied via a property if the loading process is currently not active. 因此,我决定,如果当前未激活加载过程,则用于处理控件值更改的方法仅调用通过属性提供的回调。 Otherwise, I call this.forceUpdate() in order to rerender the host component which includes setting the selectedIndex property of the segmented control. 否则,我调用this.forceUpdate()以重新渲染主机组件,其中包括设置分段控件的selectedIndex属性。 However, when this.forceUpdate is invoked the selected index does not change. 但是,调用this.forceUpdate时,所选索引不会更改。 Therefore, I decided to print the value selectedIndex is set to which always displays the initial (wished) index. 因此,我决定打印将selectedIndex设置为始终显示初始(希望的)索引的值。

Below you can see the code of my rendering method for displaying the segmented control: 在下面,您可以看到用于显示分段控件的rendering方法的代码:

render: function()
{
    ...
    var indexBluetoothAutostart = 0;
    switch (this.props.data.BluetoothAutostartState)
    {
        case BluetoothAutostartState.OFF:
            indexBluetoothAutostart = 1;
            break;
        case BluetoothAutostartState.ON:
            indexBluetoothAutostart = 2;
            break;
        case BluetoothAutostartState.AUTO:
            indexBluetoothAutostart = 0;
            break;
    }
    console.log("index: " + indexBluetoothAutostart);
    ...
    return (
        ...
        <SegmentedControlIOS selectedIndex={indexBluetoothAutostart}
            momentary={false}
            onValueChange={(newValue) => this.changeBluetoothAutostartState(newValue)}
            values={[Localization.HomeFragment.BluetoothAutostartState.btnAuto, Localization.HomeFragment.BluetoothAutostartState.btnOff, Localization.HomeFragment.BluetoothAutostartState.btnOn]}/>
    );
}

Finally, the following snippet shows the onValueChange handler: 最后,以下代码片段显示了onValueChange处理程序:

changeBluetoothAutostartState: function(newValue)
{
    if (newValue === Localization.HomeFragment.BluetoothAutostartState.btnAuto)
    {
        if (this.props.bluetoothConnectionBusy === true)
        {
            Toast.display(Localization.BluetoothOperationInProgress.msgWaitForUploadToFinishToast);
            this.forceUpdate();
        }
        else
        {
            this.props.onBluetoothAutostartStateChange(BluetoothAutostartState.AUTO);
        }
    }
    // process the other two options
    ...
}

I appreciate any help. 感谢您的帮助。 Thanks in advance. 提前致谢。

我只是简单地将一个用'Date.now()'初始化的时间戳变量添加到类的状态,而不是调用'this.forceUpdate'我更新了状态的时间戳:

this.setState({ timestamp: Date.now() });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM