简体   繁体   English

React Native Progress View iOS 不重新渲染

[英]React Native Progress View iOS Doesn't Rerender

I am building an app in React Native & REDUX.我正在用 React Native 和 REDUX 构建一个应用程序。 I am using the Progress View iOS in React Native to show a very simple native looking progress bar.我在 React Native 中使用 Progress View iOS 来显示一个非常简单的原生进度条。 Here is the code:这是代码:

renderProgressBar() {
    if(!!Platform.OS) {
        switch(Platform.OS) {
            case 'ios':
                return (
                    <ProgressViewIOS
                        progress={(this.props.backgroundData.percentageLoaded || 0)}
                        progressTintColor={globalProgressBarTintColor}
                        trackTintColor={globalProgressBarEmptyTrackTintColor} />
                );

            case 'android':
                return (
                    <ProgressBarAndroid
                        color={globalProgressBarTintColor}
                        progress={(this.props.backgroundData.percentageLoaded || 0)} />
                );

            default:
                return null;
        }

    } else {
        return null;
    }
}

I have paused the app in it's REDUX container as well as in this render method of the view itself and I am 100% positive that all of the progress values are making it in fine.我已经在它的 REDUX 容器以及视图本身的渲染方法中暂停了应用程序,我 100% 肯定所有的进度值都很好。 They show up from the reducer in my container being passed into the view.它们从我的容器中的减速器中出现,并被传递到视图中。 They show up in all render methods as well.它们也出现在所有渲染方法中。 They are 0, 0.2, 0.4, 0.666666601, 0.8 and 1.它们是 0、0.2、0.4、0.666666601、0.8 和 1。

So what I am saying is that this.props.backgroundData.percentageLoaded will equal these values I have just mentioned.所以我要说的是this.props.backgroundData.percentageLoaded将等于我刚刚提到的这些值。 I can pause it and see that the value is certainly there and it is a Number not a string so all should be well.我可以暂停它并看到该值肯定存在并且它是一个数字而不是一个字符串所以一切都应该很好。 For some reason, even though it renders 5 times, nothing shows up.出于某种原因,即使渲染了 5 次,也没有显示任何内容。

It is not behind something else (speaking UI stack wise), because I have set the progress value to 1 and set the colours to red and blue in the bar and it shows up fine.它并不落后于其他东西(就 UI 堆栈而言),因为我已将进度值设置为 1,并将栏中的颜色设置为红色和蓝色,并且显示正常。 If I keep those crazy red/blue colours and set the progress to all those values I mentioned above they all work individually (hardcoded not the dynamic prop value).如果我保留那些疯狂的红色/蓝色并将进度设置为我上面提到的所有这些值,它们都可以单独工作(硬编码而不是动态道具值)。

In case anyone wants to know the only React Native lifecycle method I am using is the render and it looks like this:如果有人想知道我使用的唯一 React Native 生命周期方法是渲染,它看起来像这样:

render() {
    this.titleWidth = this.props.deviceWidth / 2;

    return (
        <View>
            <View style={[
                            styles.navbar,
                            {
                                width: this.props.deviceWidth,
                                justifyContent: (this.props.showBackIcon === false && this.props.showingTitle === false) ? 'flex-end' : 'space-between',
                            }
                        ]}>

                { this.props.showBackIcon ? this.renderBackButton() : null }

                { this.props.showingTitle ? this.renderTitle() : null }

                { this.props.showingIcon ? this.renderRightIconButton() : null }

                { this.props.showingTextButton ? this.renderRightTextButton() : null }

            </View>

            { this.renderProgressBar() }
        </View>
    );  
}

I am stumped.我难住了。 Thoughts anyone?有人想吗? Thanks.谢谢。

Edit: Also, incase it helps, here is the container code being sent in as props:编辑:另外,如果有帮助,这里是作为道具发送的容器代码:

const mapStateToProps = (state, props) => {
    return {
        ...safelyBuildPassProps(state.navigation.route.passProps),
        deviceHeight: state.appSettings.deviceOrientation.currDeviceHeight,
        deviceWidth: state.appSettings.deviceOrientation.currDeviceWidth,
        backgroundData: {
            loadingData: state.appSettings.loadingBackgroundData,
            percentageLoaded: state.appSettings.loadingBackgroundDataPercent,
        },
    }
}

Second Edit第二次编辑

I should explain more that this is all based on ASync calls and transformations.我应该解释一下,这都是基于 ASync 调用和转换的。 All of the processes happen in the client (device) and the processes are these:所有流程都发生在客户端(设备)中,流程如下:

  • Make call to backend with fetch使用 fetch 调用后端
  • Get response object with success status and send to modelling service获取具有成功状态的响应对象并发送到建模服务
  • Modelling service (which is a local ASync function) cleans response object and translates into proper UI results with colours and whatnot建模服务(它是一个本地 ASync 函数)清理响应对象并转换为带有颜色等的正确 UI 结果
  • Once iterating over the array of response objects, divide 100 by length of array and then again by 100 to get the decimal value for the ProgressViewIOS to use.遍历响应对象数组后,将 100 除以数组长度,然后再除以 100 以获得要使用的ProgressViewIOS的十进制值。
  • Dispatch the percentage just after finishing the transformations on each object在完成每个对象的转换后立即发送百分比
  • Dispatch calls rerender on the navbar where the progress bar is and hands it the new percentage as shown in code above. Dispatch 在进度条所在的导航栏上调用 rerender 并将新的百分比传递给它,如上面的代码所示。
  • Rerender happens (I have paused it here to see it returning proper JSX object and percentages) but nothing on the screen changes in the device重新渲染发生(我在这里暂停它以查看它返回正确的 JSX 对象和百分比)但设备屏幕上的任何内容都没有变化

The problem here was that I was attempting to update the progress bar about 20 times in less than a second.这里的问题是我试图在不到一秒的时间内更新进度条大约 20 次。 It's great to have a progress bar to see the progress on your data crunching, however... if the entire transformation occurs in under a second... you probably don't require a progress bar.有一个进度条来查看数据处理的进度是很棒的,但是......如果整个转换发生在一秒钟内......你可能不需要进度条。

Once I added more time in there, the progress bar had time to update just fine.一旦我在那里添加了更多时间,进度条就有时间更新了。

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

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